Beginner's guide to ActiveRecord Querying
class Products < ApplicationRecord
end
user = User.create(name: "", occupation: "")
user.name
user.occupation
user = User.new do |u|
u.name = ''
u.occupation = ''
end
users = User.all # returns all users
user = User.first # returns the first user
parm = User.find_by(name: "Parm") # returns a specified user
user = User.find_by(name: "Parm") # find the user
user.name = "Kishan" # update the name field
user.save # save the new changes to the database
user = User.find_by(name: "Parm")
user.update(name: "Kishan")
User.update_all "occupation = students, field2 = blah2"
user = User.find_by(name: "Parm") # find the user
user.destroy # destroy the record
find
: argument is the id of the record you wish to return
Client.find(10)
# is eqivalent to the sql query: SELECT * FROM clients WHERE (clients.id = 10) LIMIT 1
You can find more than one clients by passing an array of id's to be searched for:
Client.find([1,10,9,4]) # = SELECT * FROM clients WHERE (clients.id IN (1,10))
find_by
: argument is the column name of the record you wish to return
Client.find_by(name: "lil") # is equivalent to:
Client.where(name: "lil").take
SELECT * FROM clients WHERE (clients.name = 'lil') LIMIT 1
WHERE
: allows you to use specify conditions to narrow down your search!
Client.where("field1 = ?", params[:field1])
Client.where("created_at >= :start_date AND created_at <= :end_date", {start_date: params[:start_date]
, end_date: params[:end_date]})
Client.where(locked: true) # SELECT * FROM clients WHERE clients.locked = true
Article.where(author: author) # author is the AR object, will return all articles that belong to this author
Author.joins(:articles).where(articles: {author: author})
Client.where(id: [1,2,3]) =
SELECT * FROM clients WHERE clients.id IN (1,2,3)
Client.where.not(locked: true)
Client.order(:field)
Client.order(created_at: :asc)
Client.order(created_at: :desc)
# you can chain order after a subseqent order
Client.select("column1", "column2", ...) # SELECT column1, column2 FROM clients
GROUP BY
clause to the sql fired by a finder method:
Order.select("column1", "column2").group("column1")
Order.select().group().having("condition")
Author.joins("INNER JOIN posts ON posts.author_id = authors.id AND posts.published = 't'")
Category.joins(:articles)
SELECT categories.* FROM categories INNER JOIN articles ON articles.category_id = categories.id
Client.joins(:orders).where("orders.created_at" => ...)
Author.left_outer_joins(:posts).select("")...
clients = Client.limit(10) # 1 SQL Call
clients.each do |client|
puts client.address.postcode # 10 SQL calls
end # total of 11 sql calls
clients = Client.includes(:address).limit(10) # 1 SQL call
clients.each do |client|
puts client.address.postcode # 1 SQL call
end # total of 2 calls
Article.includes(:category, :comments)
scope :name, -> {query}
scope :published, -> { where(published: true)}
def self.published
where(published: true)
end
Article.published
scope :name, ->(arg) {}
Client.select(:col1, :col2, etc...)
# which runs: SELECT col1, col2 FROM clients
Person
.select()
.join()
.where()
SELECT ...
FROM people
JOIN table name ON table name.person_id = people.id
WHERE ...
Client.find_by_sql("SELECT ... FROM ... WHERE ... ORDER BY...")