Hello, If user --> has_many :address How can we do a build_address as it expects a has_one relationship? Thanks, Avi -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/FzVdpnprVj0J. For more options, visit https://groups.google.com/groups/opt_out.
On 11 December 2012 09:49, Avi <aavinash.behera-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > If user --> has_many :address > > How can we do a build_address as it expects a has_one relationship?@user.addresses.build( ... ) Have a look at the Rails Guide on ActiveRecord Associations. It shows all the methods available for each association type. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hai! View this link http://guides.rubyonrails.org/association_basics.html by bdeveloper01 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Dec 11, 2012 at 3:49 AM, Avi <aavinash.behera-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If user --> has_many :address > > How can we do a build_address as it expects a has_one relationship?The association is created by a foreign key on addresses which should be `user_id` unless you want a custom field (for example I would prefer it to be uid so I tell them that is the case but you need to read the links provided by others to see how to do that.) Then to the easiest way to create records on the association is: User.find_by_email("name-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org").addresses.new Though again, for the sake of context and readability I prefer to also alias all my associations to their singular form for creation because to me it makes no sense to do User.new.addresses.new when you are only creating a single address, it only makes sense when pulling the them. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.