gavin.robertson1986-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-Jan-28 15:02 UTC
Setting association values via id or objects
Hi there, I''ve got a question about best practice/preferred method of setting up an association. I have a User model which has and belongs to many interests. From playing about with the console I know I can go.. interests = Interests.find([1,2,3,4]) user = User.new(:interests=>interests) ...or... user = User.new(:interest_ids=>[1,2,3,4,5]) and similarly on existing objects user.interest_ids = [1,2,3,4] or user.interests = [interest_obj_a, interest_obj_b, interest_obj_c) My question is, is there a functional difference between the two, or can one freely interchange between setting relations via id or via literal objects? Thanks, Gavin. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
gavin.robertson1986-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> Hi there, > > I''ve got a question about best practice/preferred method of setting up > an association. I have a User model which has and belongs to many > interests. > > From playing about with the console I know I can go.. > > interests = Interests.find([1,2,3,4]) > user = User.new(:interests=>interests) > ...or... > user = User.new(:interest_ids=>[1,2,3,4,5]) > > and similarly on existing objects > > user.interest_ids = [1,2,3,4] > or user.interests = [interest_obj_a, interest_obj_b, interest_obj_c) > > My question is, is there a functional difference between the two, or > can one freely interchange between setting relations via id or via > literal objects? > > Thanks, >There is no difference in the results, where there might be a difference is in how many calls to the database each takes. Do which ever is more straight forward to start, however. By the time it makes a performance difference you may have ditched the HABTM relationship for something saner anyway. -- 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 -~----------~----~----~----~------~----~------~--~---