通过使用rails on ruby​​ on join的文本框从多个表中获取数据

时间:2014-03-13 12:23:17

标签: ruby-on-rails

在我的搜索页面中,我有两个文本框," tf_Zip"和" tf_Designation"在哪里我输入" Zip"和用户的指定,我有两个模型,如下:

user's model 
id | name | user_Zip
1  | abc  | 10005
2  | mno  | 10005
3  | xyz  | 10005

profession's model
id | UserID | Designation
1  | 1      | Lead
2  | 2      | software engineer
3  | 3      | Lead

我希望当我输入user_Zip i.e 10005 in tf_Zip textbox and Designation i.e Lead in tf_Designation textbox时,它会给我那些user_Zip为10005且Designation = Lead且用户1和3为user_Zip为10005且Designation = Lead且我正在使用的用户下面的代码:

def search
@user_zipdesignation = Profession.joins(:user).where('users.user_Zip' => params[:tf_Zip] 'and professions.Designation' => params[:tf_Designation])
end

在代码中我使用了user_Zip和Designation的两个条件,但它给了我以下错误:

C:/Sites/MentorMentored1/app/controllers/search_controller.rb:32: syntax error, unexpected tSTRING_BEG, expecting ')' ....user_Zip' => params[:tf_Zip] 'and professions.Designation' ... ... ^ C:/Sites/MentorMentored1/app/controllers/search_controller.rb:32: syntax error, unexpected tASSOC, expecting keyword_end ...and professions.Designation' => params[:tf_Designation]) ... ^ C:/Sites/MentorMentored1/app/controllers/search_controller.rb:32: syntax error, unexpected ')', expecting keyword_end C:/Sites/MentorMentored1/app/controllers/search_controller.rb:50: syntax error, unexpected $end, expecting keyword_end

我不明白我错过的地方')' 。请帮助我,等待回复。感谢

2 个答案:

答案 0 :(得分:1)

这有用吗?

def search
  @user_zipdesignation = Profession.joins(:user).where('users.user_Zip' => params[:tf_Zip],  'professions.Designation' => params[:tf_Designation])
end

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

答案 1 :(得分:0)

def search
  @user_zipdesignation = Profession.joins(:user).where('users.user_Zip = ? AND professions.Designation= ?',params[:tf_Zip],params[:tf_Designation])
end

并观察您的开发日志以获取SQl语句