On 6 December 2011 14:27, Bill Walton <[email protected]> wrote:
> Before I call this a bug, would someone tell me if I'm missing something here?
>
> Using 1.9.2 and 3.1.1... I can create a child if the relationship is
> has_many, but not if the relationship is has_one.
>
> rails new association_test
> cd association_test
>
> rails g model Poppa name:string
> rails g model OnlyChild name:string poppa_id:integer
> rails g model Kid name:string poppa_id:integer
>
> ...
>
> ruby-1.9.2-p290 :006 > p.only_child.create(:name => 'sam')
>  OnlyChild Load (0.4ms)  SELECT "only_children".* FROM
> "only_children" WHERE "only_children"."poppa_id" = 1 LIMIT 1
> NoMethodError: undefined method `create' for nil:NilClass

I think you have to use create_association for a has_one rather than create. See
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
p.only_child returns a record (or tries to) and so cannot have a
create method that does what you want.

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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to