Active Record
ruby
Model.find(id)
Model.find_by(key: value)
Model.find([1, 10])
Model.first
Model.first(3)
Model.last
Model.order(:key).first
Model.where(key: 'value')
Model.where('value' => true)
Model.all.each do |item|
# logic with item
end
Model.find_each do |item|
# logic with item
end
Model.where(key: value).find_each do |item|
# logic with item
end
Model.find_each(key: value) do |item|
# logic with item
end
Model.find_in_batches do |items|
# logic with items (batches of 1000)
end
Model.where("column = ?", value)
Model.where("column1 = ? AND column2 = ?", value1, value2)
Model.where("created_at >= :start_date AND created_at <= :end_date",
{start_date: valuu1, end_date: value2]})
Model.where("column LIKE ?", value + "%")
model1 = ModelOne.first
ModelTwo.where(model: model1)
ModelOne.joins(:model_twos).where(model_twos: { model: model1 })
Model.where(created_at: (Time.now.midnight - 1.day)..Time.now.midnight)
Model.where(created_at: (Time.now.midnight - 1.day)..)
Model.where(column: [1,3,5])
Model.where.not(value: [1,3,5])
Model.where(column: 'value').or(Model.where(column2: [1,3,5]))
Model.column # all models
Model.pluck(:column)
# or
Model.pluck(:column, :column2)
Model.select(:column).map { |c| c.column }
# or
Model.select(:column).map(&:column)
# or
Model.select(:column, :column2.map { |c| [c.column, c.column2] }
Model.select(:first_name).map &:name
Model.ids # all ids