搜索rails中的select_tag?

时间:2014-04-06 12:40:16

标签: ruby-on-rails search full-text-search html-select

我有三个模型Lcitieslservices以及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 %>

1 个答案:

答案 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")

不确定您期望得到什么样的结果,但答案与您的问题相符。