开发者

has_many _through not working in Rails 3 after upgrade from Rails 2

开发者 https://www.devze.com 2023-03-10 01:18 出处:网络
I have the following in my controller: @campaign = Campaign.where(:id => params[:id]) @companies = @campaign.companies.sort { |a,b| a.name <开发者_Go百科=> b.name` }

I have the following in my controller:

@campaign = Campaign.where(:id => params[:id])
@companies = @campaign.companies.sort { |a,b| a.name <开发者_Go百科=> 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


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

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 a companies method.

You either want to call .first on the result set:

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

Or just use .find:

@campaign = Campaign.find(params[:id])
0

精彩评论

暂无评论...
验证码 换一张
取 消