1

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

4

1 回答 1

2
@campaign = Campaign.where(:id => params[:id])

返回一个结果数组(可能只有一个项目,但仍然是一个数组)。您收到的“无方法错误”是因为Array该类没有companies方法。

您要么想调用.first结果集:

@campaign = Campaign.where(:id => params[:id]).first

或者只是使用.find

@campaign = Campaign.find(params[:id])
于 2011-05-31T23:49:23.380 回答