2011-09-28 3 views
2

복잡한 양식을 만들기 위해 accepts_nested_attributes를 사용하려고합니다. Nested Attributes 문서, examples 등을 바탕으로, 그래서 같은 모델을 설정 한 :accepts_nested_attributes_ 미정의 메소드 오류

사용자 모델 :

require 'digest' 
class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :first_name, :last_name, :email, :password, 
        :password_confirmation, :ducks_attributes 

    has_many :ducks, :class_name => 'Duck' 
    accepts_nested_attributes_for :ducks 
    . 
    . 
    . 
end 

오리 모델 :

class Duck < ActiveRecord::Base 
    belongs_to :user 
    accepts_nested_attributes_for :user 
end 

하지만하려고 할 때 콘솔에서 중첩 된 속성에 액세스하면 얻을 수 있습니다.

ruby-1.9.2-p290 :003 > User.first.ducks_attributes 
NoMethodError: undefined method `ducks_attributes' for #<User:0x007ffc63e996e0> 
    from ~/.rvm/gems/[email protected]/gems/activemodel-3.0.9/lib/active_model/attribute_methods.rb:392:in `method_missing' 
    from ~/.rvm/gems/[email protected]/gems/activerecord-3.0.9/lib/active_record/attribute_methods.rb:46:in `method_missing' 
    from (irb):3 
    from ~/.rvm/gems/[email protected]/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start' 
    from ~/.rvm/gems/[email protected]/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start' 
    from ~/.rvm/gems/[email protected]/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top (required)>' 
    from script/rails:6:in `require' 
    from script/rails:6:in `<main>' 

내가 틀렸어? 미리 감사드립니다.

+0

FWIW, 레일즈 3.0.9 – Dan

답변

2

속성 작성자 만 정의됩니다.

class User < ActiveRecord::Base 
    has_many :ducks 
    accepts_nested_attributes_for :ducks 
end 

class Duck < ActiveRecord::Base 
    belongs_to :user 
end 

# This works: 
User.first.ducks_attributes = [ { :name => "Donald" } ] 

# This is more common (attributes posted from a form): 
User.create :ducks_attributes => [ { :name => "Donald" }, { :name => "Dewey" } ] 
+0

고마워요, Molf- 그게 다예요. – Dan

관련 문제