Created
April 22, 2015 13:13
-
-
Save mikedao/491ab04b622bd5fe5cbd to your computer and use it in GitHub Desktop.
AR Challenges
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
2-1 Order.first.items << Item.first | |
2-2 | |
2-3 | |
before_destroy :destroy_orders | |
def destroy_orders | |
self.orders.delete_all | |
end | |
2-4 | |
Order.order(:created_at)[10..19] | |
2-5 | |
Order.all.collect {|order| order.items.map {|item| item if item.id == 1}}.map {|order| order.compact}.flatten | |
1-1 | |
items = Item.arel_table | |
Item.where(items[:name].matches("%rubber%")) | |
1-2 | |
Order.all.inject([]) {|result, order| result << [order, order.items.count]}.sort_by {|entry| entry[1]}.reverse[0..4] | |
1-3 | |
Order.order(:created_at)[0..9] | |
1-4 | |
def self.last_month # Show only products of last month. | |
where(:purchase_date => 1.month.ago.beginning_of_month..1.month.ago.end_of_month) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment