Created
August 20, 2013 22:07
-
-
Save laurenhavertz/6287970 to your computer and use it in GitHub Desktop.
This file contains 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
scope: attached to the model. Can be used in any part of rails or can be included with any association | |
ex. ` scope: completed, lambda { where(completed: true) } | |
# @list.items.completed.alphabetically | |
scope: completed. | |
collection proxy: Finders | |
- `class Item < AR::Base ` | |
- inherits from active record | |
- generates sql | |
- ex. can use Item.all (select * from items;) -> [] | |
- Item.where(completed: true) (ruby way to generate SQL) | |
* Select all from Item where complete = 'true' -> [] | |
- @items = Item.all | |
- if current_user | |
@items = @items.where ({ user_id => current_user.id }) => #to_a IR #each (ways to loop) | |
ex. 2 ` scope : created_before, lambda { |date| where("created_at > ?", date)} | |
end | |
* scope :search, -> { |query| | |
search_terms = query.split(" ") | |
search_terms = search_terms.map { |term| term.downcase } | |
like_clause = search_terms.map { |term| "name LIKE('%(term)%')" } | |
where(where_clauses.join(" OR ")) | |
3. Delegates: allow you to access method on associated object in model | |
ex. @user = User.find(1) | |
if @user.linkedin_token.profile | |
# | |
delegate :linkedin_profile, :to => :linkedin_token, :allow_nil => true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment