【问题标题】:Rails search - including multiple tables in the same index searchRails 搜索 - 在同一索引搜索中包括多个表
【发布时间】:2021-02-14 16:28:35
【问题描述】:

我是一名初学者,正在努力让搜索功能在我的 rails 应用程序上运行。我有两个对象 - 用户和个人资料。我希望能够搜索用户或个人资料中存在的属性,在本例中为电子邮件和姓氏。

我目前收到此错误:

SQLite3::SQLException: no such column: last_name: SELECT "users".* FROM "users" WHERE (last_name LIKE '%test')

这是我的代码:

架构

create_table "profiles", force: :cascade do |t|
    t.integer  "user_id"
    t.string   "first_name"
    t.string   "last_name"
    t.string   "job_title"
    t.string   "phone_number"
    t.string   "contact_email"
    t.string   "description"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.string   "avatar_file_name"
    t.string   "avatar_content_type"
    t.integer  "avatar_file_size"
    t.datetime "avatar_updated_at"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.integer  "plan_id"
    t.string   "stripe_customer_token"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end 

users_controller.rb

class UsersController < ApplicationController
   
   def index
      @users = User.includes(:profile).map{ |user| user.profile }.flatten

      if params[:search]
         @users = User.includes(:profile).search(params[:search]).order("created_at DESC")
      else
         @users = User.all.order('created_at DESC')
      end
   end
   
   # GET request made to /users/:id
   def show
    @user = User.find( params[:id] )
   end
end

user.rb

has_one :profile 

def self.search(search)
    where("email LIKE ?", "%#{search}%")
    where("last_name LIKE ?", "%#{search}")
end

profile.rb

belongs_to :user

index.html.erb

<div class="search">
                 <br/>
                 <div class="md-form mt-0">
                     <%= form_tag(users_path, :method => "get", id: "search-form") do %>
                    <%= text_field_tag :search, params[:search], placeholder: "Search profiles" %>
                    <%= submit_tag "Search" %>
                    <% end %>
                </div>
                
            </div> 
            <ul class="list-unstyled">
                <% @users.each do |user| %>
                    <% if user.profile %>
                        <li>
                            <div class="well row <%= cycle('white-bg', '') %>">
                                <div class="col-sm-4 text-center">
                                    <% if user.profile.avatar %>
                                        <%= link_to user do %>
                                        <%= image_tag user.profile.avatar.url, class: 'user-index-avatar' %>
                                        <% end %>
                                    <% end %>
                                </div>
                                <div>
                                    <%= link_to user do %>
                                    <h2><%= user.profile.first_name %> <%=user.profile.last_name%></h2>
                                    <% end %>
                                    <p><%= user.profile.job_title %></p>
                                    <p><%= user.profile.description %></p>
                                </div>
                            </div>
                        </li>
                    <% end %>
                <% end %>
            </ul>
        </div>

我尝试了搜索方法主体的许多不同变体。我尝试过包含和连接功能,但似乎没有任何效果。当我注释掉这部分时:

where("last_name LIKE ?", "%#{search}")

它通过电子邮件地址搜索来工作。

任何帮助将不胜感激。提前致谢!

【问题讨论】:

    标签: ruby-on-rails search


    【解决方案1】:

    我想说问题是您必须指定要查询的表。您可能应该这样做:

    @user = User.joins(:profile)
                .where('users.email LIKE ?', "%#{search}%")
                .where('profiles.last_name LIKE ?', "%#{search}%")
    

    【讨论】:

    • 谢谢!这主要是现场。我将其更新为:@user = User.joins(:profile) .where('users.email LIKE ? OR profiles.last_name LIKE ?', "%#{search}%","%#{search}%") 我还必须在用户控制器中更新users.DESC 并且它有效。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多