【问题标题】:Rails search in two modelsRails 搜索两种模型
【发布时间】:2014-09-01 06:55:11
【问题描述】:

我是 Rails 新手,我想进行搜索,我使用了 ransack 但没有成功。我在导航栏中有一个文本字段来查找我的应用程序

布局/_navigation.html.erb

<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  <div class="container">

    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <div class="header">
        <img src="logo.png" alt="logo" />
        <a class="navbar-brand" href="/">BLABLABLA</a>
      </div>
    </div>

 <div class="navbar-collapse collapse">
   <%= search_form_for @search do |f| %>
          <div class="navbar-form navbar-right" role="form">
            <div class="form-group">
                <%= f.search_field :nombre_cont, class: "form-control" %> 
            </div>
              <%= f.submit "Buscar" , class: "btn btn-success btn-primary" %>
            </div>
       <% end %> 
    </div> <!--/.navbar-collapse -->

  </div>

将搜索在我的 HomeController 中的两个模型的字段中输入的任何单词

db/schema.rb

ActiveRecord::Schema.define(:version => 20140324120403) do

  create_table "departamentos", :force => true do |t|
    t.string   "nombre"
    t.integer  "interno"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "funcionarios", :force => true do |t|
    t.string   "nombre"
    t.string   "apellido"
    t.string   "cargo"
    t.integer  "departamento_id"
    t.integer  "interno"
    t.string   "correo"
    t.date     "cumpleanio"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
  end

end

这将是我想要的 SQL 查询

SELECT funcionarios.* 
FROM funcionarios 
INNER JOIN departamentos ON departamentos.id = funcionarios.departamento_id 
WHERE 
(funcionarios.nombre LIKE '%VALUE%' OR 
funcionarios.apellido LIKE '%VALUE%' OR 
departamentos.nombre LIKE '%VALUE%' OR
departamentos.interno LIKE '%VALUE%')

知道如何搜索吗?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    试试下面的怎么样:

    Funcionario.joins(:departamentos).where("nombre like '%?%' OR apellido LIKE '%?%' OR departamentos.nombre LIKE '%?%' OR departamentos.interno LIKE '%?%'", VALUE, VALUE, VALUE, VALUE)
    

    希望对你有帮助。

    【讨论】:

    • 非常感谢。我应该在控制器中的方法中或作为模型在哪里写该行,并且可以在我的视图中使用?抱歉,我问了这些问题,我使用 Rails 有一段时间了,但还是完全不明白:(
    • @Adh761 完全没问题!如果需要,您可以在控制器中使用它,但更好的方法是将其作为范围放在 Funcionario 模型中 => guides.rubyonrails.org/active_record_querying.html#scopes 。我希望这会有所帮助:)。
    • 好的,谢谢,我在模型中创建了搜索方法,在我的 HomeController 索引 @Funcionarios = Funcionarios.search.(params [:q]) 中,然后我将放入 layout / _navigation.html.erb?
    • 是的,你可以在你的视图中使用这个@funcionarios 实例变量,只要你想要它。
    • 类似于此答案的内容正确 [stackoverflow.com/questions/15611869/…
    猜你喜欢
    • 2012-09-20
    • 2013-12-13
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2017-12-06
    相关资源
    最近更新 更多