ActiveRecord cheat sheet / EXAMPLES
$ gem install activerecord in GEMFILE: gem ‘activerecord’
require ‘active_record’
HOW TO USE
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => 'database_path')
ActiveRecord::Base.connection.execute <<-SQL CREATE TABLE IF NOT EXISTS students ( id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR NOT NULL, last_name VARCHAR NOT NULL, ); SQL
one-to-one:
class Customer < ActiveRecord::Base has_one :detail end
class Detail < ActiveRecord::Base belongs_to :customer end
has_one :detail after_initialize do self.build_detail end
has_one :detail, :dependent => :destroy
delegate :birthday, :gender, :city, :to => :detail OR delegate *Detail::ATTR_METHODS, :to => :detail
(must include ATTR_METHODS = [:birthday, :birthday=, :gender, :gender=, :city, :city=] in detail class to use 2nd option]
one-to-many
class Parent < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :parent
end
has_many :children, :dependent => :destroy
many-to-many
class Magazine < ActiveRecord::Base has_many :subscriptions has_many :customers, :through => :subscriptions end
class Customer < ActiveRecord::Base has_many :subscriptions has_many :magazines, :through => :subscriptions end
@customer = customer.find(17)
@customer.magazines
@customer.subscriptions
Single/Parent:
myStudent1 = Student.new(:first_name => 'Ann', :last_name => 'Smith', :birthday => '1986-08-11')
myStudent1.save
Child:
(myStudent1.classes.new(:name => ‘Geometry’)).save
##note alternative method for saving and pluralized ‘classes’##
Find one by id:
Customer.find(17)
Find related records by id:
Customer.find(17).detail.birthday
Find by label:
Can do:
- Person.where(:user_name => user_name).first
- Person.where(:last_name => last_name).all
- Person.where(:user_name => user_name, :password => password).first
Better:
- Person.find_by_user_name(user_name)
- Person.find_all_by_last_name(last_name)
- Person.find_by_user_name_and_password(user_name, password)
List all:
Contact.all.each do |contact| puts contact.first_name end
Conditional:
Student.where(:gender => 'male')
LIKE:
Contact.where("first_name LIKE 'A%'") OR Contact.where(‘first_name LIKE ?', “A%")
both examples above find contacts where first_name column begins with A. Some people prefer second for stylistic reasons
Student.where("birthday LIKE '%-07-%'").each do |student| puts student.first_name + " has a birthday in July" end
Count:
Client.count
## prints count of number of records in Client table
Client.where(:first_name => 'Ryan').count
Unique:
Client.select(:name).uniq
contacts = Address.where(:state => 'CA').includes(:contact).collect do |address| address.contact end
contacts.uniq.each do |contact| puts contact end
Another way doing this (via http://whatcodecraves.com/articles/2009/04/08/adventures-with-active-record-find):
Fruit.all(:select => "DISTINCT fruits.*", ...)