Skip to content

Instantly share code, notes, and snippets.

@scottlingran
Created September 17, 2012 15:52
Show Gist options
  • Save scottlingran/3738141 to your computer and use it in GitHub Desktop.
Save scottlingran/3738141 to your computer and use it in GitHub Desktop.
Model queries
### find things in your table
Model.find_by_<key>(value) # Limits 1
Model.where(:key => value) # No limit, returns all
example = Model.find_by_<key>(key)
example.key = 'value' # this will return the key/value and update it
### find/create by
Model.find_or_create_by_<key>(key)
### Order in Model
Model.order("<key> DESC")
Model.reorder("<key> DSEC") #overrides default scope
### Get unique values
Products.pluck(:category).uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment