Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ralph Johnson
10,408 PointsTesting for a nil record across a join table association
I have 3 tables, named Jobs, Agents(STI from Users—employees, basically, but not W-2 employees--rather, 1099 "independent agents") and a join table called Jhooks. Agents have_many Jobs, and Jobs have_many agents, :through => :jhooks. I populated the database with a few samples, and tested the relationships out in the console.
current_user = Agent.first
j = Job.first
j.agents.include?(current_user)
=> false
current_user = Agent.find(5)
j.agents.include?(current_user)
=> true
!j.agents.include?(current_user)
=> false
This all worked perfectly—the relationships work, and the queries (including the last one, with the bang for "not") ran without an error. In the cases where the console returned "false," there was no Jhook record--the class instance was nil
Now I want my available_jobs view to display only jobs where the Agent has not yet selected an availability (the Jhook column is :state, via state_machine, and has various options, including but not limited to “available” and “unavailable”). I don't know yet what behavior that will cause--an "unavailable" :state value may still return "true" because a Jhook association exists, but that's a problem for later because right now I can't find a conditional statement to specify that the Agent has not yet chosen an availability.
But once it works, I only want to display, in the view, Jobs where the Agent (defined as the current_user in sessions) has no Jhook record—where the return is [], or nil.
In the controller:
def available_jobs
@jobs = Job.active
end
...which is essentially:
@jobs = Job.all.where('start_date > ?', Date.today)
(which leaves out the necessary condition searching for a nil record in Jhook for an association between the Job and the Agent)
So in the model, the I've filled in the first condition (but still need the second half of the && conditional):
scope :active, -> { where("start_date >= ?", Date.today) }
I have tried a gazillion variations on the second half, essentially trying to define the current_user as having NO Jhook record--[] or nil—but I've been trying for hours to frame the query and they all throw exceptions (so naturally the view won't render).
I'm stumped. Anybody have any ideas?
1 Answer
Ralph Johnson
10,408 PointsThis worked, in case anyone else has the same problem:
@jobs = Job.all.select { |j| !j.agents.include?(current_user) && j.start_date >= Date.today }
...probably because passing a block allowed "j"to represent the job instance, so method calls worked.