在rails的select_tag上搜索? [英] Search on select_tag in rails?
问题描述
我有三个模型 Lcities
和 lservices
和搜索
class Lcity< ActiveRecord :: Base
has_many:Lservices
attr_accessible:lname,:lcode,:lexperience,:lrating,:llocation
end
和
class Lservice< ActiveRecord :: Base
belongs_to:Lcity
attr_accessible:lcode,:lscode,:lcharg,:lname
end
class搜索< ActiveRecord :: Base
attr_accessible:city,:services
end
提交后在搜索表单
我希望Lcities模型中的所有 lname
我知道sql查询,但是如何在 Rails
>从lcity中选择lname其中llocation.Lcity = lname.Lservice
form_for search
<%= form_for(@search)do | f | %GT;
<%= f.select(:city,city_for_select,:prompt =>'选择城市')%>
<%= f。选择(:service,service_for_select,:prompt =>'选择服务')%>
<%end%>
首先,您的符号必须被降低:
has_many:lservices
和
belongs_to:lcity
其次,你的SQL稍微从右到左......而且,它没有任何意义。然而,为了满足定制者,它应该是(注意表格的复数形式):
从lcites,lservices中选择lname其中lcites .location = lservices.lname
您的查询:
Lcity.joins(:lservices).where(lservices.lname = lcites.llocation)
不知道你期待什么样的结果,但答案与你的问题相符。
I have three models Lcities
and lservices
and search
class Lcity < ActiveRecord::Base
has_many :Lservices
attr_accessible :lname , :lcode , :lexperience , :lrating , :llocation
end
and
class Lservice < ActiveRecord::Base
belongs_to :Lcity
attr_accessible :lcode , :lscode , :lcharg , :lname
end
class Search < ActiveRecord::Base
attr_accessible :city , :services
end
After submitting in Search form
i want the all the lname
from the Lcities model i know the sql query but how to apply on Rails
>select lname from Lcity where llocation.Lcity = lname.Lservice
form_for search
<%= form_for (@search) do |f| %>
<%= f.select(:city, city_for_select, :prompt => 'Select City') %>
<%=f. select(:service, service_for_select, :prompt => 'Select Services') %>
<% end %>
To start with, your symbols have to be downcased:
has_many :lservices
and
belongs_to :lcity
Second, your SQL is slightly right-to-left... Also, it makes little to no sense. However, to satisfy the custommer, it should be (mind the plural for tables):
select lname from lcites,lservices where lcites.llocation = lservices.lname
For your query:
Lcity.joins(:lservices).where("lservices.lname = lcites.llocation")
Not sure what kind of results you're expecting, but the answer matches your question.
这篇关于在rails的select_tag上搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!