我试图在查询后植入过滤器。我有2个模型与协会1)露营2)Caracteristiquetest。 Modele Caracteristiquetest有camping_id。查询工作,但当我尝试应用带复选框的过滤器时,我有一个错误。我认为这是一个关联问题。
我如何解决这个问题?谢谢你的帮助。
SQLite3 :: SQLException:没有这样的栏目:障碍:SELECT" campings"。* 来自"露营"在哪里(piscine LIKE' %%'和烧烤一样' %%' AND nomdep LIKE'%aube%'和障碍喜欢'%oui%')
Camping.rb
scope :handicap, -> (handicap) { where handicap: "oui" }
belongs_to :caracteristiquetest
def self.search(piscine, barbecue, nomdep, handicap)
return scoped unless piscine.present? || barbecue.present? || nomdep.present? || handicap.present?
where(['piscine LIKE ? AND barbecue LIKE ? AND nomdep LIKE ? AND handicap LIKE ?', "%#{piscine}%", "%#{barbecue}%", "%#{nomdep}%", "%#{handicap}%"])
end
Caracteristiquetst.rb
has_one :camping
camping_controller.rb
def resultnohome
if params[:piscine] && params[:barbecue] && params[:nomdep] && params[:handicap].blank?
redirect_to action: :index and return
else
@campings = Camping.search(params[:piscine], params[:barbecue], params[:nomdep], params[:handicap])
end
end
_searchfilter.html.erb
<%= text_field_tag :nomdep, params[:nomdep], class:"SearchFilter", placeholder:"Ex : Vendée, Corse..." %>
<li><p><span class="IcoSwim" aria-hidden="true"></span> Piscine : <%= check_box_tag :piscine, "oui", !!params[:piscine], :class => "piscine" %></p></li>
<li><p><span class="IcoSlide" aria-hidden="true"></span>Toboggan aquatique </p></li>
<li><p><span class="glyphicon glyphicon-fire" aria-hidden="true"></span> Barbecue : <%= check_box_tag :barbecue, "oui", !!params[:barbecue], :class => "barbecue" %></p></li>
<li><p><span class="IcoHandi" aria-hidden="true"></span> Accès handicapé : <%= check_box_tag :handicap, "oui", !!params[:handicap], :class => "handicap" %></p></li>
<%= submit_tag "Appliquer les filtres", class:"btn btn-danger2", name: nil %>
一些精确度:通过鱼类或烧烤工作进行过滤,因为它们处于相同的迁移状态,相同的模型但是障碍物不能正常工作。
露营桌的结构
class CreateCampings < ActiveRecord::Migration[5.0]
def change
create_table :campings do |t|
t.string :name
t.string :adresse
t.string :code_postale
t.string :commune
t.string :courriel
t.string :site_internet
t.string :tel
t.text :description
t.string :nomdep
t.string :nomregion
t.string :numdep
t.string :slug
t.integer :ville_id
t.integer :region_id
t.integer :departement_id
t.float :latitude
t.float :longitude
t.string :etoile
t.string :piscine
t.string :barbecue
t.integer :user_id
t.integer :caracteristiquetest_id
t.timestamps
end
end
end
特征表的结构
class CreateCaracteristiquetests < ActiveRecord::Migration[5.0]
def change
create_table :caracteristiquetests do |t|
t.string :handicap
t.integer :camping_id
t.timestamps
end
end
end
在google上进行一些搜索之后,我认为.joins可以加入两个表来解决问题。所以我在camp.rb上做了这个。
def self.searchi(nomdep, handicap)
return scoped unless nomdep.present?
joins(:caracteristiquetest).where(['nomdep LIKE ? OR name LIKE ? AND handicap LIKE ?', "%#{nomdep}%", "%#{nomdep}%", "%#{handicap}%"])
end
但是,现在我还有另一个错误......
SQLite3 :: SQLException:没有这样的列:campings.camping_id:SELECT &#34;露营&#34;。* FROM&#34;露营&#34; INNER JOIN&#34; caracteristiquetests&#34;上 #&34; caracteristiquetests&#34;&#34; ID&#34; =&#34;露营&#34;。&#34; camping_id&#34;在哪里(nomdep 喜欢&#39;%露营%&#39;或者命名为“露营%&#39;%和障碍LIKE&#39; %%&#39;)
解决这个问题的方法是什么?谢谢你的帮助。
答案 0 :(得分:0)
我在表格结构中看不到handicap
字段。所以错误信息是不言自明的。
答案 1 :(得分:0)
你正试图从露营和障碍中得到结果是一系列特征测试。 添加字段&#39;障碍&#39;露营。
rails g migration addHandicapToCamping handicap
答案 2 :(得分:0)
我找到了解决方案,问题在于关联。
Camping.rb
has_many :caracteristiquetests
Caracteristiquetest.rb
belongs_to :camping
我在我的模型上添加它后,我可以使用另一个表中的参数进行过滤。 left_outer_joins(:caracteristiquetests).where(mywhere)