问题
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