Last active
August 29, 2015 14:03
-
-
Save davidlfox/405ac6e8ea087697b545 to your computer and use it in GitHub Desktop.
need AR query for null parent field or non-null parent field and no related child records
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
def search | |
@chores = Chore.available | |
if search_params[:search_text] | |
@chores = @chores.where('description LIKE ?', "%%%s%" % search_params[:search_text]) | |
elsif search_params[:zipcode] | |
@chores = @chores.where('location LIKE ?', "%%%s%" % search_params[:zipcode]) | |
end | |
@chores = @chores.order(created_at: :desc).all | |
render 'index' | |
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
Chore Load (0.2ms) SELECT "chores".* FROM "chores" INNER JOIN "taken_chores" ON "taken_chores"."chore_id" = "chores"."id" WHERE (NOT (deadline is not null)) AND (description LIKE '%keyword%') ORDER BY "chores"."created_at" DESC |
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
class Parent < ActiveRecord::Base | |
has_many :children | |
scope :available, -> { joins(:taken_chore).where.not('deadline is not null', :taken_chores => { :chore_id => !nil })} | |
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
select * from chores | |
where chores.deadline is null | |
OR (chores.deadline is not null AND | |
(select count(*) from taken_chores where taken_chores.chore_id=chores.id) = 0) | |
order by chores.created_at desc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment