1

我正在尝试在我的一个模型中实现搜索。我正在使用 gem “mongoid_search”,并且在搜索时将它与 railscast #240 混合。它似乎返回一个空数组,我不知道为什么。谁能指出我正确的方向?

我的模型如下所示:

include Mongoid::Search 

attr_accessible :twitterUsername, :twitterName, :twitterLocation, :twitterDescription, :projects

def self.search(search)
    if search
        search_in :twitterUsername, :twitterName, :twitterLocation, :twitterDescription, :projects
    else
        scoped
    end
end

我的控制器:

def index
    @users = User.search(params[:search])

    respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @users }
    end
end

而我的观点:

<%= form_tag users_path, :method => 'get', :id => "users_search" do %>
    <p>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Search", :name => nil %>
    </p>
<% end %>

更新

我也在模型中定义了 twitterUsername(抱歉命名约定不好):

field :provider, :type => String
field :uid, :type => String
field :twitterName, :type => String
field :twitterUsername, :type => String
field :twitterAvatar, :type => String
field :twitterUrl, :type => String
field :twitterPortfolioUrl, :type => String
field :twitterLocation, :type => String
field :twitterDescription, :type => String
field :email, :type => String
field :description, :type => String
field :portfolioUrl, :type => String
field :watchers, :type => Integer
field :inspirations, :type => Integer
field :invitees, :type => Integer
field :shot, :type => String
field :remote_image_url, :type => String
field :hireMe, :type => Boolean
field :projects, :type => Integer
key :twitterUsername


def self.create_with_omniauth(auth)
    create! do |user|
        user.provider = auth['provider']
        user.uid = auth['uid']
        if auth['info']
            user._id = auth['info']['nickname'] || ""
            user.twitterName = auth['info']['name'] || ""
            user.twitterUsername = auth['info']['nickname'] || ""
            user.twitterAvatar = auth['info']['image'] || ""
            user.twitterUrl = auth['info']['urls']['Twitter'] || ""
            user.twitterPortfolioUrl = auth['info']['urls']['Website'] || ""
            user.twitterLocation = auth['info']['location'] || ""
            user.twitterDescription = auth['info']['description'] || ""
        end
    end
end

更新 2

如果我计算用户数,它似乎返回了一个用户(这是正确的,因为我是现在唯一注册的用户):

<% if @users.count > 0 %>
  <%= @users.count %>
<% end %>

如果我做:

<% if @users.count > 0 %>
  <%= @users.twitterUsername %>
<% end %>

它给了我这个错误:用户:类的未定义方法`twitterUsername'。

更新 3

当我尝试运行 tail log/development.log 时,它给了我一个错误:

± % tail log/development.log                                                                                                             
22:     
app/views/users/index.html.erb:19:in `block in _app_views_users_index_html_erb__1766206529136141992_2490506940'
app/views/users/index.html.erb:18:in `each'
app/views/users/index.html.erb:18:in `_app_views_users_index_html_erb__1766206529136141992_2490506940'
app/controllers/users_controller.rb:7:in `index'


Rendered /Users/holgersindbaek/.rbenv/versions/1.9.3-p125-perf/lib/ruby/gems/1.9.1/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (5.3ms)
Rendered /Users/holgersindbaek/.rbenv/versions/1.9.3-p125-perf/lib/ruby/gems/1.9.1/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
Rendered /Users/holgersindbaek/.rbenv/versions/1.9.3-p125-perf/lib/ruby/gems/1.9.1/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (13.9ms)

更新 4

如果当我运行搜索时,我会在控制台中得到这个:

Started GET "/users?utf8=%E2%9C%93&search=holgersindbaek" for 127.0.0.1 at 2012-04-10     23:24:55 +0200
Processing by UsersController#index as HTML
Parameters: {"utf8"=>"✓", "search"=>"holgersindbaek"}
MONGODB (1ms) flow04_development['users'].find({:_id=>"holgersindbaek"}).limit(-1).sort([[:_id, :asc]])
Rendered users/index.html.erb within layouts/application (4.7ms)
Completed 500 Internal Server Error in 8ms

ActionView::Template::Error (undefined method `id' for User:Class):
22:         <% if current_user == user %>
23: 
24:         <% else %>
25:           <% if current_user.follower_of?(user) %>
26:             <%= link_to "Unfollow", unfollow_user_path(user), :remote => true,     :class=>'unfollow' %>
27:           <% else %>
28:             <%= link_to "Follow", follow_user_path(user), :remote => true, :class=>'follow' %>
app/views/users/index.html.erb:25:in `block in _app_views_users_index_html_erb__83132873031271873_2505118720'
app/views/users/index.html.erb:18:in `each'
app/views/users/index.html.erb:18:in `_app_views_users_index_html_erb__83132873031271873_2505118720'
app/controllers/users_controller.rb:7:in `index'
4

2 回答 2

0

在我看来,问题在于 mongoid-search 在保存文档时会在文档中添加一个名为“_keywords”的属性。然后它根据“_keywords”搜索文档

因此,您实际上需要重新索引集合。尝试这个:

添加到用户模型

field :reindexed, type: Boolean, default: false

运行以下

User.where(reindexed: false).each {|u| u.reindexed = true; u.save}

那么当你搜索时,它应该是快乐的。

于 2012-04-10T16:06:51.443 回答
0

我最终没有在 if/else 语句中进行搜索。最终做了:

search_in :twitterUsername, :twitterName, :twitterLocation, :twitterDescription

就如此容易。我正在使用索引进行搜索,因为我没有将它用于其他任何事情。

于 2012-04-17T17:01:12.253 回答