Hi all, I''m wondering what the best practices/conventions are for passing parameters to models I am writing. Is it better to pass ActiveRecord objects, or their database IDs? In my current application, I''ve been passing mostly ActiveRecord objects, because I''m generally using some attributes of that object. Passing the ID and then doing a find all over again seems like a waste. On the other hand, I''m finding that most of the code that I''m pushing out of the controllers into my model is likely to have IDs, since users are often submitting forms with the object IDs. In those cases, I''m doing an extra find in the controller to get the object to pass to the model, which in many cases is simply doing a DB call using the ID as a condition. So, my question is, what is recommended by the experts? 1. Pass only ActiveRecord objects 2. Pass only database IDs 3. Pass whatever is more efficient for the DB in the particular use case (this seems like a headache for overall application consistency) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-26 19:12 UTC
Re: Better to pass Activerecord objects or ids as parameters?
On Feb 26, 6:40 pm, Jay Bo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all, > > I''m wondering what the best practices/conventions are for passing > parameters to models I am writing. Is it better to pass ActiveRecord > objects, or their database IDs? >I personally would pass activerecord objects. Sometimes you might waste a little if you do just use that object for it''s id but I wouldn''t worry about that too much. Apart from anything else doing the find validates that the id does actually correspond to a real object. You might consider skipping it for something that''s really on the hot path but I really wouldn''t do that from the outset. Fred> In my current application, I''ve been passing mostly ActiveRecord > objects, because I''m generally using some attributes of that object. > Passing the ID and then doing a find all over again seems like a waste. > > On the other hand, I''m finding that most of the code that I''m pushing > out of the controllers into my model is likely to have IDs, since users > are often submitting forms with the object IDs. In those cases, I''m > doing an extra find in the controller to get the object to pass to the > model, which in many cases is simply doing a DB call using the ID as a > condition. > > So, my question is, what is recommended by the experts? > > 1. Pass only ActiveRecord objects > 2. Pass only database IDs > 3. Pass whatever is more efficient for the DB in the particular use case > (this seems like a headache for overall application consistency) > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---