Ruby on Rails Wednesday, May 27, 2015

Hello,

If I am understanding correctly, you need some mechanism to correlate several models together, you are hoping to use the created_at and modified_at timestamps provided by active record, to match the models using a timestamp.

I see the created_at and modified_at timestamp's as database level information - I try not to rely on these for my business logic as they represent when the database record was created and modified, this is a separate concern to the actual business transaction. 

Anything in my business domain that I want to record a time against I try model that explicitly. In this case I would consider introducing a transaction_completed_at column (or a UUID). Or take it a step further and model a transaction separately and add a foreign key in each of the other models.

The way I see it, transaction_completed_at will tell me when the transaction completed and created_at and modified_at will tell me when it was persisted  and modified in the database.

Thanks,
Henry 

On 27 May 2015 at 00:28, Ruby Railhead <rubyrailhead@gmail.com> wrote:
Hello, Frederick:

Thanks for your response, that was very helpful both in validating my approach, and suggesting other ways to accomplish this.

I'm still surprised this issue hasn't come up for others ... does anyone else have input on how this should/shouldn't be performed, or how they solved this issue? 

(For those just tuning in, I need all the updated_on/created_on timestamps, in a series of AR object created/updated within a transaction, to match exactly)

Thanks again,
  Ruby



On Thursday, May 21, 2015 at 5:20:59 AM UTC-4, Frederick Cheung wrote:


On Thursday, May 21, 2015 at 6:58:35 AM UTC+1, Ruby Railhead wrote:

Idea 3: Subclass ActiveRecord::Base (to say ActiveRecordLocal) and mix in the modules mentioned above into this class. 
Thoughts: This makes is clearer that when you're using ActiveRecordLocal, you're not getting the stock ActiveRecord::Base behavior. Otherwise this solution is identical to Idea 2.


Of the ideas you outline, I would either do this or:

- use SomeModel.record_timestamps = false to stop active record setting timestamps
- use before_save / before_create callbacks  (or override save) to set your own timestamps
- continue overriding transaction in order to get your start of transaction timestamp. 

The reason for this slightly different strategy is that it means you're only overriding public methods, so their api will remain stable (or at least go through the usual deprecation cycle) across versions of active record.

Fred 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4674c558-dbba-4b48-9f27-ed9f1ac00406%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Mobile: +61 4 0136 1909

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPeHLo%3DUTjZe736vxJrjKmm%3DHrQmtcVQYYpB1jaFvVcUQ%2Bi-WQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment