0

我試圖通過控制檯將用戶添加到用戶表:U = User.new但我總是得到一個錯誤未定義的方法`的has_many」 ActiveRecord的:模塊我嘗試了許多解決方案,但沒有任何幫助,所以我試圖刪除的has_many關係OT避免我得到同樣的錯誤,但與未定義的方法「attr_accessor」或者我拿到的時候我刪除attr_accessor 未定義的方法「before_save」錯誤。我還有另外4個模型是消息發佈後的友情評論。NoMethodError:未定義的方法`的has_many「ActiveRecord的:模塊

這裏是我的用戶模型

class User < ActiveRecord:: 


    has_many :posts 
    has_many :comments 
    has_many :messages 
    has_many :friendships 
    has_many :friends , through=> :friendships 
    attr_accessor :email, :password 
    before_save :encrypt_password 
    after_save :clear_password 
    validates_prescence_of :first_name 
    validates_prescence_of :last_name 
    validates :password, presence: true, length: { minimum: 6 } 
    validates_prescence_of :email 
    validates_uniqueness_of :email , uniqueness: { case_sensitive: false } 
    validates_prescence_of :gender 
    validate :that_born_on_is_not_in_the_future 



    def self.authenticate(email, password) 
     user = find_by_email(email) 
     if ((user && user.password_hash) == (BCrypt::Engine.hash_secret(password, user.salt))) 
     user 
     else 
     nil 
     end 
    end 

    def encrypt_password 
     if password.present? 
     self.salt = BCrypt::Engine.generate_salt 
     self.hash = BCrypt::Engine.hash_secret(password, salt) 
     end 
    end 

    def clear_password 
    self.password = nil 
    end 


    def that_born_on_is_not_in_the_future 
     self.errors.add :birth_date, 'is in the future' \ 
    unless self.birth_date <= Date.today end 


    end 

在此先感謝。

回答

2

這可能是導致該錯誤的原因。

class User < ActiveRecord:: 

ActiveRecord是一個模塊,所述類是Base。它應該是

class User < ActiveRecord::Base 
+0

我完全相信這是正確的答案。注意到立即自己:) –

+0

是的,答案是正確的。 –

+0

@Swards我試過,但它導致另一個錯誤,這是** NameError:未定義的局部變量或方法'通過'用戶(呼叫'User.connection'建立一個連接):類**,當我通過關係它造成了另一個錯誤! – Beeee

相關問題