从 Rails 2 升级后,has_many _through 在 Rails 3 中不起作用
我的控制器中有以下内容:
@campaign = Campaign.where(:id => params[:id])
@companies = @campaign.companies.sort { |a,b| a.name <=> b.name` }
第二行为我提供了公司的未知方法,并且之前工作正常。
这是我的活动模型中的情况:
has_many :companies, :through => :contacts, :uniq => true
我尝试了以下操作,但仍然没有解决问题:
has_many :companies, :through => :联系人, :uniq =>正确,:来源=> :公司
I have the following in my controller:
@campaign = Campaign.where(:id => params[:id])
@companies = @campaign.companies.sort { |a,b| a.name <=> b.name` }
The second line gives me an unknown method for companies
and it worked fine before.
This is in my campaign model:
has_many :companies, :through => :contacts, :uniq => true
I tried the following and it still didn't fix it:
has_many :companies, :through => :contacts, :uniq => true, :source => :company
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回一组结果(可能只是一个项目,但仍然是一个数组)。您收到的“无方法错误”是因为
Array
类没有companies
方法。您要么想在结果集上调用
.first
:或者只使用
.find
:returns an array of results (probably just one item, but still an array). The "No Method Error" you're receiving is because the
Array
class doesn't have acompanies
method.You either want to call
.first
on the result set:Or just use
.find
: