Rails ActiveRecord Queries

Some of the basic ways to use ActiveRecord to find

Queries using ActiveRecord



# Simple queries return collections of instances
	Product.all
	Product.find(3) # id=3
	Product.w­her­e(a­uthor: 'admin')
	Product.where("likes_count = ?", value_for_count)
	Product.f­ind­_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