samedi 25 avril 2015

callback before_save seems not working

I need execute the method name before the record is saved in the database, but it seems not working

class Guest < ActiveRecord::Base
  before_save :name
  has_and_belongs_to_many :checkins
  validates_presence_of :FirstName
  validates_presence_of :LastName
  validates_format_of :FirstName, with: /^[A-Za-z\s]+$/
  validates_format_of :LastName, with: /^[A-Za-z\s]+$/

  def name
    self.FirstName.titleize  + " " + self.LastName.titleize  
  end
end

console

=> Guest(id: integer, FirstName: string, LastName: string, age: integer, sex: string, photo: string, address: text, mobile: integer, email: string, birthdate: date, created_at: datetime, updated_at: datetime) 
1.9.3-p547 :008 > f=Guest.new
 => #<Guest id: nil, FirstName: nil, LastName: nil, age: nil, sex: nil, photo: nil, address: nil, mobile: nil, email: nil, birthdate: nil, created_at: nil, updated_at: nil> 
1.9.3-p547 :009 > f.FirstName=" fernando "
 => " fernando " 
1.9.3-p547 :010 > f.LastName=" suarez"
 => " suarez" 
1.9.3-p547 :011 > f.save
 => true 
1.9.3-p547 :012 > Guest.last
 => #<Guest id: 9, FirstName: " fernando ", LastName: " suarez", age: nil, sex: nil, photo: nil, address: nil, mobile: nil, email: nil, birthdate: nil, created_at: "2015-04-26 00:16:38", updated_at: "2015-04-26 00:16:38"> 

Aucun commentaire:

Enregistrer un commentaire