我有三个模型Lcities
和lservices
以及search
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 Search < ActiveRecord::Base
attr_accessible :city , :services
end
在Search form
中提交后我想要Lcities模型中的所有lname
我知道sql查询但是如何申请 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 %>
答案 0 :(得分:0)
首先,你的符号必须是低级的:
has_many :lservices
和
belongs_to :lcity
其次,你的SQL稍微从右到左......而且,它几乎没有任何意义。但是,为了满足定制者,它应该是(记住表格的复数形式):
select lname from lcites,lservices where lcites.llocation = lservices.lname
查询:
Lcity.joins(:lservices).where("lservices.lname = lcites.llocation")
不确定您期望得到什么样的结果,但答案与您的问题相符。