I am trying to add user to table User by console : u=User.new but I always get an error undefined method `has_many' for ActiveRecord:Module and I tried many solutions but nothing helped so I tried to remove has_many relations ot avoid the error I get the same error but with undefined method 'attr_accessor' or I get when I remove attr_accessor undefined method 'before_save' . i have another 4 models which are message-post-friendship-comment.
here is my User Model
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
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire