简体   繁体   English

Rails:获取has_and_belongs_to_many关联的记录

[英]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? 如果articlestemplates具有has_and_belongs_to_many关系,我将如何重写此请求以将articles提供给current_user 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! 谢谢你的帮助!

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: 不确定brand协会代表什么并发布您的模型将有助于找出这一点很长,但这是一个猜测:

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

You were pluralizing templates in the where clause but not in the join. 您在where子句中使用了复数templates ,但在联接中没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM