在rails中我构建请愿模型的方式出了什么问题?

时间:2011-12-14 20:37:59

标签: ruby-on-rails ruby activerecord

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
似乎我的多对多关系不起作用,为什么?

1 个答案:

答案 0 :(得分:1)

按照惯例,模型名称是单数。正如您可以通过错误看到的那样,当您拥有Signature时,它正在寻找Signatures。您应该将模型名称更改为Signature。此外,在Signatures中,您在belongs_to关系中使用复数名称 - 它们也应该是单数。

class Signature < ActiveRecord::Base
  belongs_to :signer, :class_name => 'User'
  belongs_to :petition
end