开发者

rails activerecord metasearch undefined method

I'm new to rails and need your开发者_JAVA技巧 help for what should be a simple problem.

when I run this code:

@search = User.search(params[:search])
@foundusers = @search.all.paginate :page => params[:page], :per_page => 20
@user = @search.where("users.id = ?", params[:id])
if @user.nil?
  @user = @foundusers.first
end
unless @user.company_id.nil?
  @company = Company.find(@user.company_id)
end

I get following error statement: undefined method `company_id' for #

I dont understand because the query on the database is correct (SELECT users.* FROM users LEFT OUTER JOIN companies ON companies.id = users.company_id WHERE (((users.firstname LIKE '%s%' OR users.lastname LIKE '%s%') OR companies.name LIKE '%s%')) AND (users.id = '11'))

and the company_id for this user is not empty.

When I type puts @user.name, instead of giving me the user name, it gives me 'User'

Many thanks for your help.


Although your database 'users' table has a column called 'company_id' the User model has a property just called 'company'.

Try unless @user.company_id.nil? @company = Company.find(@user.company.id) end


thanks for your help. Actually, I solved the issue by changing the model name @user into @myuser. I didn't change anything else and it works. My guess is that the issue is due to the Devise plug-in but it's just a guess...

Cheers.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜