Reputation: 1370
I'm working on applying a conditional in my Regions show page based on the careers that are present. I only want to show that region's careers OR the ones on the HQ.
So I put in my helper
def career_region
current_career = @careers
hq = Career.joins(:region).where(regions: {name: "HQ"})
current_career.or(hq)
end
So I end up with
Relation passed to #or must be structurally compatible. Incompatible values: [:joins]
Now I know I could do:
hq = Region.find_by(name: "HQ").careers
But this has me curious to know how I can do the joins where and then do an or by @careers.
Is there a way to do a joins where OR instance variable?
Upvotes: 0
Views: 2912
Reputation: 1264
As the error message specifies, the problem is that @careers
relation doesn't have the joins
with :region
, and hq
relation does have the joins
. When ActiveRecord
construct the SQL query, it will have a syntax error.
Fortunately, the fix is very simple, you just need to add the joins to @careers
, just like this:
def career_region
current_career = @careers
hq = Career.joins(:region).where(regions: {name: "HQ"})
current_career.joins(:region).or(hq)
end
Upvotes: 3