Cannot create child through association

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

rake db:migrate

class Poppa < ActiveRecord::Base
has_one :only_child
has_many :kids
end

class Kid < ActiveRecord::Base
belongs_to :poppa
end

class OnlyChild < ActiveRecord::Base
belongs_to :poppa
end

rails c # ( linebreaks added for readability )

Loading development environment (Rails 3.1.1)
ruby-1.9.2-p290 :001 > Kid
=> Kid(id: integer, name: string, poppa_id: integer, created_at:
datetime, updated_at: datetime)

ruby-1.9.2-p290 :002 > OnlyChild
=> OnlyChild(id: integer, name: string, poppa_id: integer,
created_at: datetime, updated_at: datetime)

ruby-1.9.2-p290 :003 > Poppa
=> Poppa(id: integer, name: string, created_at: datetime, updated_at:
datetime)

ruby-1.9.2-p290 :004 > p = Poppa.create(:name => ‘pops’)
SQL (24.4ms) INSERT INTO “poppas” (“created_at”, “name”,
“updated_at”) VALUES (?, ?, ?) [[“created_at”, Tue, 06 Dec 2011
14:18:54 UTC +00:00], [“name”, “pops”], [“updated_at”, Tue, 06 Dec
2011 14:18:54 UTC +00:00]]
=> #<Poppa id: 1, name: “pops”, created_at: “2011-12-06 14:18:54”,
updated_at: “2011-12-06 14:18:54”>

ruby-1.9.2-p290 :005 > p.kids.create(:name => ‘bob’)
SQL (0.7ms) INSERT INTO “kids” (“created_at”, “name”, “poppa_id”,
“updated_at”) VALUES (?, ?, ?, ?) [[“created_at”, Tue, 06 Dec 2011
14:19:29 UTC +00:00], [“name”, “bob”], [“poppa_id”, 1], [“updated_at”,
Tue, 06 Dec 2011 14:19:29 UTC +00:00]]
=> #<Kid id: 1, name: “bob”, poppa_id: 1, created_at: “2011-12-06
14:19:29”, updated_at: “2011-12-06 14:19:29”>

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 from /home/wildbill/.rvm/gems/ruby-1.9.2-p290@auction/gems/activesupport-3.1.1/lib/active_support/whiny_nil.rb:48:inmethod_missing’
from (irb):6
from
/home/wildbill/.rvm/gems/ruby-1.9.2-p290@auction/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in
start' from /home/wildbill/.rvm/gems/ruby-1.9.2-p290@auction/gems/railties-3.1.1/lib/rails/commands/console.rb:8:instart’
from
/home/wildbill/.rvm/gems/ruby-1.9.2-p290@auction/gems/railties-3.1.1/lib/rails/commands.rb:40:in
<top (required)>' from script/rails:6:inrequire’
from script/rails:6:in `’
ruby-1.9.2-p290 :007 >

Huh? Any insight would be appreciated. It’s early here yet, so maybe
I just need a fresh cup. Is this a bug? My Google-fu doesn’t seem to
be warmed up yet, cause I can’t find anything.

TIA,
Bill

On 6 December 2011 14:27, Bill W. [email protected] wrote:

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

p.only_child returns a record (or tries to) and so cannot have a
create method that does what you want.

Colin

HI Colin,

On Tue, Dec 6, 2011 at 8:46 AM, Colin L. [email protected]
wrote:

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
ActiveRecord::Associations::ClassMethods
p.only_child returns a record (or tries to) and so cannot have a
create method that does what you want.

Yep. That was it. Thanks. If I think about it real hard and hold my
mouth just right it makes a little bit of sense :wink:

Best regards,
Bill