我有两个模型,下面是这些模型的模式:
user's model schema:
id | First_Name
1 | abc
2 | xyz
profession's model schema:
id | UserID | Designation
1 | 1 | Lead
2 | 1 | software engineer
3 | 2 | Lead
我有文本框,我在搜索页面输入用户的“指定”,当我在“指定”文本框中输入“Lead”并点击提交按钮时,它给了我user's First_Name and Designation
其名称等于“领先”。以下是我的查看页面代码:
<table>
<tr>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">Specs</h2>
<table>
<tr>
<td align="center">
<input type="text" name="tf_Designation" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="button">
<input type="submit" name="search" value="Search" class="buttonSearch">
</div>
</td>
</tr>
<tr>
<td>
<div class="button">
<%= button_to "Back", '/default', { method: :get, :class => "buttonBack" } %>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
以下是模型:
Profession's model:
class Profession < ActiveRecord::Base
belongs_to :user
validates :UserID, presence: true
end
User's model
class User < ActiveRecord::Base
has_many :professions, dependent: :destroy
end
我在下面用它来做:
def search
if ( !params[:tf_Designation].blank? or params[:tf_Designation] !="" )
@user_designation = User.joins(:professions).where(:professions => {:Designation => params[:tf_Designation].strip}).collect(&:First_Name)
end
render 'search'
end
但它在下面给了我错误:
SQLite3::SQLException: no such column: professions.user_id: SELECT "users".* FROM "users" INNER JOIN "professions" ON "professions"."user_id" = "users"."id" WHERE "professions"."Designation" = 'Lead'
但在上述错误中,它使用"professions"."user_id"
,但在我的专业模型中,我使用的是"UserID"
而不是"user_id"
。那么如何将"user_id"
更改为"UserID"
请建议我等待您的回复。感谢。
答案 0 :(得分:0)
profession = Profession.find(:all,:conditions=>["designation=?",params[:search_key]], :select=>"user_id")
pro_users = User.find profession
first_names = pro_users.map(&:first_name)
params[:search_key]
将搜索指定项目。
答案 1 :(得分:0)
在您的控制器中使用以下代码:
@first_names = User.joins(:professions).where(:professions => {:designation => params[:tf_Designation].strip}).collect(&:first_name)