Ruby/ActiveRecord/Search

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

A find_or_create_by_* dynamic finder

=begin
drop database Contact;
create database Contact;
use Contact;
CREATE TABLE Employee (
   id  int unsigned not null auto_increment primary key,
   Name VARCHAR(50),
   Phone VARCHAR(15),
   departmentId int
);
 
CREATE TABLE Department(
   id    int unsigned not null auto_increment primary key,
   Name VARCHAR(50),
   Phone VARCHAR(15)
 
);
 
=end
require "rubygems"
require "activerecord"
ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :username => "root",
  :database => "Contact")
class Employee < ActiveRecord::Base
   set_table_name "employee"
   belongs_to :department
end
class Department < ActiveRecord::Base
   set_table_name "department"
end
account = Employee.new
account.Name = "AAA"
account.save
account = Employee.find_or_create_by_Name("B")
puts account.destroy



Find by column name

=begin
create database Contact;
use Contact;
CREATE TABLE Employee (
   Name VARCHAR(50),
   Phone VARCHAR(15)
);
=end
require "rubygems"
require "activerecord"
ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :username => "root",
  :database => "Contact")
class Employee < ActiveRecord::Base
   set_table_name "employee"
   set_primary_key "Name"
end
account = Employee.find_by_Name("AAA")
puts account.Name



Find by conditions

=begin
create database Contact;
use Contact;
CREATE TABLE Employee (
   Name VARCHAR(50),
   Phone VARCHAR(15)
);
=end
require "rubygems"
require "activerecord"
ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :username => "root",
  :database => "Contact")
class Employee < ActiveRecord::Base
   set_table_name "employee"
   set_primary_key "Name"
end
 
account = Employee.find(:all, :conditions => ["Name  = ?", "AAA"])
p account



Find first or find all

=begin
drop database Contact;
create database Contact;
use Contact;
CREATE TABLE Employee (
   id  int unsigned not null auto_increment primary key,
   Name VARCHAR(50),
   Phone VARCHAR(15),
   departmentId int
);
 
CREATE TABLE Department(
   id    int unsigned not null auto_increment primary key,
   Name VARCHAR(50),
   Phone VARCHAR(15)
 
);
 
=end
require "rubygems"
require "activerecord"
ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :username => "root",
  :database => "Contact")
class Employee < ActiveRecord::Base
   set_table_name "employee"
   belongs_to :department
end
class Department < ActiveRecord::Base
   set_table_name "department"
   has_many :children, :class_name => "Employee", :foreign_key => :departmentId
end
 
account = Employee.new
account.Name = "AAA"
account.save
department = Department.find_or_create_by_Name("B")
department.children << account
department.save
Employee.find :all
# SELECT * FROM employee
Employee.find :first
# SELECT * FROM employee LIMIT 1



Find many values

=begin
drop database Contact;
create database Contact;
use Contact;
CREATE TABLE Employee (
   id  int unsigned not null auto_increment primary key,
   Name VARCHAR(50),
   Phone VARCHAR(15),
   departmentId int
);
 
CREATE TABLE Department(
   id    int unsigned not null auto_increment primary key,
   Name VARCHAR(50),
   Phone VARCHAR(15)
 
);
 
=end
require "rubygems"
require "activerecord"
ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :username => "root",
  :database => "Contact")
class Employee < ActiveRecord::Base
   set_table_name "employee"
   belongs_to :department
end
class Department < ActiveRecord::Base
   set_table_name "department"
   has_many :children, :class_name => "Employee", :foreign_key => :departmentId
end
 
account = Employee.new
account.Name = "AAA"
account.save
department = Department.find_or_create_by_Name("B")
department.children << account
department.save
 
e = Employee.find 5 is equivalent to SELECT * FROM employee WHERE (id = 5)
es = Employee.find 1, 3, 5, 7, 9 is equivalent to SELECT * FROM employee WHERE (id IN ( 1, 3, 5, 7, 9 ))



Find with AND

Account.find :all, :conditions => { :first_name =>  @first_name,
                                    :last_name => @last_name,
                                    :favorite_band_id => nil }
which would result in the following SQL query:
SELECT *
  FROM accounts
  WHERE
    first_name = "John"
    AND last_name = "Doe"
    AND favorite_band_id IS NULL



Find with conditions

Account.find :all, :conditions => "keyword = "ruby""
Account.find :all, :conditions => { :keyword => "ruby" }
Account.find :all, :conditions => [ "keyword = ?", "ruby" ]
These statements will all generate the following WHERE clause:
SELECT * FROM accounts WHERE keyword = "ruby"



Find with OR

Account.find :all, :conditions => [ "updated_on > ? OR id IN (?)",
                                    @last_update,
                                    [ 2, 3, 5, 7, 11 ] ]
It will result in the following SQL:
SELECT *
  FROM accounts
  WHERE
    updated_on > "20061214 15:29:12"
    OR id IN (2, 3, 5, 7, 11)



id is default for find(1)

=begin
drop database Contact;
create database Contact;
use Contact;
CREATE TABLE Employee (
   id  int unsigned not null auto_increment primary key,
   Name VARCHAR(50),
   Phone VARCHAR(15),
   departmentId int
);
 
CREATE TABLE Department(
   id    int unsigned not null auto_increment primary key,
   Name VARCHAR(50),
   Phone VARCHAR(15)
 
);
 
=end
require "rubygems"
require "activerecord"
ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :username => "root",
  :database => "Contact")
class Employee < ActiveRecord::Base
   set_table_name "employee"
end
class Department < ActiveRecord::Base
   set_table_name "department"
end
account = Employee.new
account.save
account = Employee.find(1)
puts account.destroy