Created
September 17, 2012 15:52
-
-
Save scottlingran/3738141 to your computer and use it in GitHub Desktop.
Model queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### 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