OK:

My User model:

require 'digest'
class User < ActiveRecord::Base
  attr_accessor :passwd
  validates :passwd, :confirmation => true,
    :length => { :within => 6..20 },
    :presence => true,
    :if => :password_required?
  validates :nickname, :uniqueness => true,
    :length => { :within => 5..50}
  has_many :comments
  has_many :entries, :class_name => "User", :foreign_key =>
"geomatics_elaboration_id"
  has_and_belongs_to_many :entries
  before_save :encrypt_new_password
  def fullname
    (self.firstname + " " + self.lastname)
  end
  def self.authenticate(nick, passwd)
    user = User.find_by_nickname(nick)
    return user if (user and user.active and user.authenticated?(passwd))
  end
  def authenticated?(passwd)
    self.password == encrypt(passwd)
  end
  protected
  def encrypt_new_password
    return if passwd.blank?
    self.password = encrypt(passwd)
  end
  def password_required?
    password.blank? or passwd.present?
  end
  def encrypt(string)
    Digest::SHA1.hexdigest(string)
  end
end

My Entry model:

class Entry < ActiveRecord::Base
  belongs_to :applicant
  belongs_to :entry_type
  belongs_to :parish
  belongs_to :elaborator, :class_name => "User", :foreign_key =>
"geomatics_elaboration_id"
  belongs_to :checker, :class_name => "User", :foreign_key =>
"geomatics_checker_id"
  has_many :comments
  has_and_belongs_to_many :users
  def find_related_applicant_by_idcard(idCard)
    self.applicant_id = Applicant.find_by_idcard(idCard).id
  end
end

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to