stevec
stevec

Reputation: 52578

Filter ActiveRecord for current user using devise where > 1 user tables?

I have an app with 2 user tables: User and Trainer (everyone is a User, and a small subset of Users are also Trainers).

I'm using devise.

I think we can filter based on User like so

User.where(user: {id: current_user.id})

Can we somehow do the same (filtering for the current_user) but in the Trainer model?

Note: to make matters slightly more complicated, what I'm actually trying to retrieve are all of the Availability slots for a Trainer (where Trainer belongs_to User and Trainer has_many Availabilities)

Hope this makes sense

Upvotes: 0

Views: 150

Answers (1)

jvillian
jvillian

Reputation: 20253

You should probably show your Trainer model. But, it seems likely you're looking for something like:

Trainer.find_by(user: current_user).availabilities

Or, perhaps, just:

current_user.trainer.availabilities

I'm not sure what this is meant to be:

Users.where(users: {id: current_user.id})

...but it looks wrong in a number of ways.

Upvotes: 1

Related Questions