find by rails code example
Example 1: find in rails
Person.find(:first)
Person.find(:first, :conditions => [ "user_name = ?", user_name])
Person.find(:first, :conditions => [ "user_name = :u", { :u => user_name }])
Person.find(:first, :order => "created_on DESC", :offset => 5)
Person.find(:last)
Person.find(:last, :conditions => [ "user_name = ?", user_name])
Person.find(:last, :order => "created_on DESC", :offset => 5)
Person.find(:all)
Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50)
Person.find(:all, :conditions => { :friends => ["Bob", "Steve", "Fred"] }
Person.find(:all, :offset => 10, :limit => 10)
Person.find(:all, :include => [ :account, :friends ])
Person.find(:all, :group => "category")
Example 2: self.find_by_id
def self.find_by_id(id)
sql = <<-SQL
SELECT * FROM dogs WHERE id = ?
SQL
DB[:conn].execute(sql, id).map(){|row|
self.new_from_db(row)
}.first
end