Skip to content

Instantly share code, notes, and snippets.

@aetherwu
Last active August 29, 2015 14:18
Show Gist options
  • Save aetherwu/6617b93383a2abe1b66b to your computer and use it in GitHub Desktop.
Save aetherwu/6617b93383a2abe1b66b to your computer and use it in GitHub Desktop.
Ruby active record
Client.find(10)
Client.first
Client.last
Client.last(:limit => 5)
Client.all(:limit => 5, :offset => 5)
User.all.each do |user|
NewsLetter.weekly_deliver(user)
end
Client.all(:conditions => { :locked => true })
Client.all(:conditions => { :created_at => (Time.now.midnight - 1.day)..Time.now.midnight})
Client.all(:order => "created_at DESC")
# OR
Client.all(:order => "created_at ASC")
Client.all(:order => "orders_count ASC, created_at DESC")
Client.all(:select => "viewable_by, locked")
Order.all(:group => "date(created_at)", :order => "created_at")
Order.all(:group => "date(created_at)", :having => ["created_at > ?", 1.month.ago])
Client.all(:joins => 'LEFT OUTER JOIN addresses ON addresses.client_id = clients.id')
Client.find_by_sql("SELECT * FROM clients
INNER JOIN orders ON clients.id = orders.client_id
ORDER clients.created_at desc")
Client.connection.select_all("SELECT * FROM clients WHERE id = '1'")
Client.exists?(1)
Client.exists?(:conditions => "first_name = 'Ryan'")
Client.count(:conditions => "first_name = 'Ryan'")
Client.average("orders_count")
Client.minimum("age")
Client.maximum("age")
Client.sum("age")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment