Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## the problem is with the Imt <> Lesson relationship.
- ## When asking for lesson.imt it errors out looking for imt_id in the table ( which doesn't exist as it's users id )
- ## So should I be using class_name, table_name, set_table_name or finder_sql or what.. ?
- ## and, it's possible to have different validations and assoications subclassed like this?
- ## e.g. all user types ( Student, Imt, etc ) validates_presence_of :first_name. But only Imt has_many :lessons..
- ## lesson.rb :
- class Lesson < ActiveRecord::Base
- validates_presence_of :instrument, :name, :cost, :status
- belongs_to :imt, :class_name => "User"
- has_many :bookings
- has_many :enrolments
- end
- ## user.rb
- class User < ActiveRecord::Base
- require 'digest/sha2'
- #validates_uniqueness_of :username
- attr_accessor :skip_password
- attr_accessor :tmp_password
- validates_confirmation_of :password, :on => :create #:if => :password_required?
- validates_length_of :password, :within => 5..40, :on => :create #:if => :password_required?
- validates_presence_of :password, :password_confirmation, :on => :create #:if => :password_required?
- validates_presence_of :first_name, :last_name
- def name
- first_name + " " + last_name
- end
- def self.getuser(id)
- find(id)
- end
- def self.authenticate(username, password)
- user = User.find(:first, :conditions => ['username = ?', username])
- if user.blank? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
- raise "Username or password invalid"
- end
- user
- end
- def password=(pass)
- self.tmp_password = pass
- unless pass.nil?
- salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
- self.password_salt, self.password_hash =
- salt, Digest::SHA256.hexdigest(pass + salt)
- end
- end
- def password
- self.tmp_password
- end
- protected
- def password_required?
- @skip_password != true
- end
- end
- class Imt < User
- has_many :lessons
- end
- class Student < User
- validates_presence_of :form_class, :student_id
- validates_uniqueness_of :student_id
- has_many :enrolments, :dependent => :destroy
- has_many :bookings, :dependent => :destroy
- def before_validation
- self.username = self.student_id
- end
- end
- class Teacher < User
- end
- class Admin < User
- end
Add Comment
Please, Sign In to add comment