【问题标题】:Search on select_tag in rails?在rails中搜索select_tag?
【发布时间】:2014-04-06 12:40:16
【问题描述】:

我有三个模型Lcitieslservicessearch

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

【问题讨论】:

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


    【解决方案1】:

    首先,您的符号必须小写:

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

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

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2011-02-06
      • 1970-01-01
      相关资源
      最近更新 更多