Skip to content

Instantly share code, notes, and snippets.

@Humoud
Last active March 30, 2016 06:45
Show Gist options
  • Save Humoud/2a6ab0bc69b6bf61682b to your computer and use it in GitHub Desktop.
Save Humoud/2a6ab0bc69b6bf61682b to your computer and use it in GitHub Desktop.
A cheat sheet for Rail's Active Record.

Assume we have model Car

# Get all cars with color red
Car.where( color: "red" )

Retrieve all users that have field 'company' not nil

  users = Spree::User.where("spree_users.company IS NOT NULL")

Retrieve user with id 1 and display only 'id' and 'email'

  Spree::User.where(id: 1).select('email')
Person.pluck(:id) # SELECT people.id FROM people
Person.uniq.pluck(:role) # SELECT DISTINCT role FROM people
Person.where(:confirmed => true).limit(5).pluck(:id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment