t56k
t56k

Reputation: 6981

Rails: get records for has_and_belongs_to_many association

How would I rewrite this request to get the articles available to the current_user if articles and templates have a has_and_belongs_to_many relationship? I feel like I'm close but I can't quite figure it out.

current_user.brand.articles.joins(:template).where(:templates => { :id => @template.id })

Thanks for your help!

Upvotes: 1

Views: 136

Answers (1)

d_ethier
d_ethier

Reputation: 3911

Not sure what the brand association represents and posting your models would go a long way to help figure this out, but here's a guess:

current_user.brand.articles.joins(:templates).where(:templates => { :id => @template.id })

You were pluralizing templates in the where clause but not in the join.

Upvotes: 1

Related Questions