2011-12-14 41 views
0
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 

我收到此錯誤:In rails我的建立請願模式的方式有什麼問題?

>Petition.first.signers 
NameError: uninitialized constant Petition::Signature 

它看起來像我的許多一對多的關係,沒有工作,爲什麼呢?

回答

1

按照慣例,型號名稱是單數。正如你所看到的錯誤,當你有Signatures時,它正在尋找Signature。您應該將您的型號名稱改爲Signature。此外,在Signatures中,您使用複數名稱的關係爲belongs_to-它們也應該是單數。

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

嗯,這是否回答你的問題?不要忘記[upvote /接受你的問題的答案](http://meta.stackexchange.com/a/5235/158402)`:)` – 2012-03-05 08:18:38

相關問題