Created
May 16, 2012 19:58
-
-
Save MikeSilvis/2713473 to your computer and use it in GitHub Desktop.
Squeel Joins
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
# This is what I was trying to do, and works. However these end up ALL being ordered by created_at. | |
# I want to almost add something to the select of my_growls & re_growls that is like a "SORT BY THIS COLUMN" field, such that I can do this | |
# Select *, sorter AS created_at | |
# On both my_growls & re_growls. However because of the IN statement I can only return the ID from both of these. | |
# Any clues? | |
def find_every_growl | |
my_growls = Growl.where(user_id: id).order(order).select(:id) | |
re_growls = Regrowl.where(user_id: id).order(order).select(:growl_id) | |
growls = Growl.where{ | |
(id.in(my_growls)) | | |
(id.in(re_growls)) | |
} | |
growls.where(type: type) if type | |
growls | |
end |
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
# I have the following two tables and I want to find both the growls and the regrowls sorted by the created_at for growls BUT also the created_at for regrowls. | |
create_table "regrowls", :force => true do |t| | |
t.integer "user_id" | |
t.integer "growl_id" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
end | |
create_table "growls", :force => true do |t| | |
t.string "type" | |
t.text "comment" | |
t.text "link" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
t.integer "user_id" | |
t.string "photo_file_name" | |
t.string "photo_content_type" | |
t.integer "photo_file_size" | |
t.datetime "photo_updated_at" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment