ねえ、私はこの機能に少し問題があります
class User < ActiveRecord::Base
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :users
attr_accessible :name
validates_presence_of :name
before_destroy :set_default_group
private
def set_default_group
self.users.each do |u|
puts u # prints out <User:0x007fd678a85cb8>
u.group = self.first
u.save
end
end
end
このエラーが発生しますNoMethodError:#Group id:18の未定義のメソッド `group'なので、私の定義の何が問題になっていますか?
エラーメッセージ
NoMethodError: undefined method `group' for #<Group id: 18>
from /Users/.../.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:407:in `method_missing'
from /Users/.../.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:149:in `method_missing'
from /Users/.../Developer/rails/.../app/models/group.rb:12:in `block in set_default_group'
from /Users/.../.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:89:in `each'
from /Users/.../.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
from /Users/.../Developer/rails/.../app/models/group.rb:10:in `set_default_group'