Ruby on rails 联想模式有问题,我可以';t在rails控制台中创建模型对象

Ruby on rails 联想模式有问题,我可以';t在rails控制台中创建模型对象,ruby-on-rails,ruby,Ruby On Rails,Ruby,我试图在rails控制台中创建对象,但在使用关联时遇到问题 因此,我有三个模型用户,事件,参与者 class User < ActiveRecord::Base has_many :events has_many :participants has_many :events, through: :participants end class Event < ActiveRecord::Base belongs_to :user has_many :partici

我试图在rails控制台中创建对象,但在使用关联时遇到问题

因此,我有三个模型
用户
事件
参与者

class User < ActiveRecord::Base
  has_many :events
  has_many :participants
  has_many :events, through: :participants
end

class Event < ActiveRecord::Base
  belongs_to :user
  has_many :participants
  has_many :user, through: :participants
end

class Participant < ActiveRecord::Base
  belongs_to :user
  belongs_to :event
end
当我运行
event=FactoryGirl.build(:event)
时,我得到以下错误

NoMethodError: undefined method `each' for #<User:0x007f95cae313d8>
    from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activemodel-4.2.3/lib/active_model/attribute_methods.rb:433:in `method_missing'...
NoMethodError:for的未定义方法'each'#
来自/Users/derman/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activemodel-4.2.3/lib/active\u model/attribute\u methods.rb:433:在'method\u missing'中。。。

我认为这是关于模型中的关联,我从用户中删除了很多:事件,并且从事件和工作中删除了
属于:用户,但我想知道为什么?

您的事件类和用户之间的关系有点不对劲。应该是
有很多:用户
而不是
:用户

class Event < ActiveRecord::Base
  belongs_to :user
  has_many :participants
  has_many :users, through: :participants
end
class事件
:用户
工厂也是相关的,你能把它包括进去吗?在旁注中,我会称之为连接模型,因为它在语义上更为正确。
class Event < ActiveRecord::Base
  belongs_to :user
  has_many :participants
  has_many :users, through: :participants
end