Lisa Klein
2008-Dec-18 07:48 UTC
Correct way to build an AR object with multiple associations?
I have a Book resource that belongs both to a User and to an Isbn. What is the recommended way of building a new Book record in this type of situation? class Book < ActiveRecord::Base belongs_to :user belongs_to :isbn end Currently I''m doing the following: @isbn = Isbn.find(...) book = @isbn.books.build(...) book.user_id = @user.id book.save Is this the way it''s supposed to be done? I know that it''s preferred to interact with dependent resources through their associations. So adding the user_id manually seems a bit clunky to me. But I''m not sure how else to do it! Thanks in advance. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Andrew Porter
2008-Dec-18 09:58 UTC
Re: Correct way to build an AR object with multiple associations?
Lisa Klein wrote:> book.user_id = @user.id > > Is this the way it''s supposed to be done? I know that it''s preferred > to interact with dependent resources through their associations. So > adding the user_id manually seems a bit clunky to me. But I''m not >Do book.user = @user instead then (ultimately it''s the same thing obviously). --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Lisa Klein
2008-Dec-18 13:37 UTC
Re: Correct way to build an AR object with multiple associations?
Oooh, that clarified a few things. Thanks Andrew! On Dec 18, 4:58 am, Andrew Porter <a...-OV3k8GMR8cdg9hUCZPvPmw@public.gmane.org> wrote:> Lisa Klein wrote: > > book.user_id = @user.id > > > Is this the way it''s supposed to be done? I know that it''s preferred > > to interact with dependent resources through their associations. So > > adding the user_id manually seems a bit clunky to me. But I''m not > > Do > > book.user = @user > > instead then (ultimately it''s the same thing obviously).--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---