Rails ActiveRecord Queries
Queries using ActiveRecord
# Simple queries return collections of instances
Product.all
Product.find(3) # id=3
Product.where(author: 'admin')
Product.where("likes_count = ?", value_for_count)
Product.find_by(title: 'Rails 4', author: 'admin')
Product.find_or_create_by(title: 'Beatles')
# Working with collections
Product.where(author: 'pito').last # choosing
Product.where(author: 'pito').order(created_at: :desc) # sorting
# Working with associated Models
Product = Product.comments.where(author: 'georg')
# What is the sql for a certain query?
Product.sample.to_sql