class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
#relationsships
has_many :petitions
#signatures
has_many :signatures
has_many :signed_petitions ,:through => :signatures , :class_name => 'Petitions' , :foreign_key => 'petition_id'
end
class Signatures < ActiveRecord::Base
belongs_to :signers, :class_name => 'User'
belongs_to :petitions
end
class Petition < ActiveRecord::Base
attr_accessible :letter, :about, :title, :goal
#relationships
belongs_to :user
#signature
has_many :signatures
has_many :signers ,:through => :signatures , :class_name => 'User'
end
我收到此错误:
>Petition.first.signers
NameError: uninitialized constant Petition::Signature
似乎我的多对多关系不起作用,为什么?
答案 0 :(得分:1)
按照惯例,模型名称是单数。正如您可以通过错误看到的那样,当您拥有Signature
时,它正在寻找Signatures
。您应该将模型名称更改为Signature
。此外,在Signatures
中,您在belongs_to
关系中使用复数名称 - 它们也应该是单数。
class Signature < ActiveRecord::Base
belongs_to :signer, :class_name => 'User'
belongs_to :petition
end