Search on select_tag in rails?

白昼怎懂夜的黑 提交于 2020-01-06 13:29:55

问题


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 %>

回答1:


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.



来源:https://stackoverflow.com/questions/22894160/search-on-select-tag-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!