简体   繁体   English

当思想狮身人面像找到数据时,您如何将数据从Rails中的ruby中的哈希中提取出来?

[英]When thinking-sphinx finds data how do you then pull data out of the hash in ruby on rails?

Started using thinking sphinx today and I'd like to know what's going wrong here: 今天开始使用思维狮身人面像,我想知道这里出了什么问题:

Here is my controller: 这是我的控制器:

class SearchesController < ApplicationController
   def index
     @user = User.search params[:search]
   end
end

view: 视图:

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


<%= @user %>

user model 用户模型

  # thinking sphinx
  define_index do
    indexes username
    indexes email
  end

browser: 浏览器:

[#<User id: 35, email: "elna@hodkiewicz.net", encrypted_password: "$2a$10$4TCbzA3FLel4uZIAyILHVOFPNqyqShDEMPpv0FHsS24I...", password_salt: "$2a$10$4TCbzA3FLel4uZIAyILHVO", username: "yasmin35", created_at: "2012-01-24 10:01:38", updated_at: "2012-01-24 10:01:38", password_reset_token: nil, password_reset_sent_at: nil, email_change_token: nil, email_change_sent_at: nil>] 

I assumed because it found the user I could pull what ever info I wanted from that hash, so I tried this just as a test: 我以为是因为它找到了我可以从该哈希中提取所需信息的用户,所以我尝试了一下作为测试:

<%= @user.username %>

and got this: 并得到了:

undefined method `username' for #<ThinkingSphinx::Search:0x0000010497e258>

I'm certain I expecting thinking sphinx to work the same way my rails find methods do but I guess they don't. 我确定我希望狮身人面像可以像我的rails find方法一样工作,但我想它们不会。 How would I achieve what I'm trying to achieve? 我将如何实现自己想要实现的目标?

I would like to find a user by username or email which is working..but once I'v got that info ..use it to access associations such as the users profile and photos... so basically just use sphinx find the user then do things the way I would without sphinx.. 我想通过有效的用户名或电子邮件来查找用户。但是一旦获得该信息,就可以使用它来访问诸如用户个人资料和照片之类的关联...所以基本上只使用sphinx查找用户,然后以没有狮身人面像的方式做事。

I'm wondering is there any point using sphinx to allow my users to search for another user by email or username..? 我想知道使用狮身人面像让我的用户通过电子邮件或用户名搜索其他用户是否有意义。 country etc.. 国家等

A user has one profile and many photos. 用户具有一个个人资料和许多照片。 I thought by finding the user first I could then take advantage of their associations without sphinx but I was wrong. 我以为先找到用户,然后我就可以利用他们的联系而无需狮身人面像,但我错了。

Someone please enlighten be. 请有人启发。 I'd really appreciate it. 我真的很感激。

Kind regards 亲切的问候

ThinkingSphinx returns an array of results by default, which is why that User object is wrapped in [ ] square brackets. ThinkingSphinx默认情况下返回结果数组,这就是为什么User对象包装在[ ]方括号中的原因。

This is the same way that a User.where(:something => true) statement works in Rails in that you need to call .first on the search or iterate over the results. 这与User.where(:something => true)语句在Rails中的工作方式相同,因为您需要在搜索中调用.first或对结果进行迭代。 Otherwise, you are calling .username on an array object which won't work. 否则,您将在无法使用的数组对象上调用.username

In this case, you should be iterating over the result set: 在这种情况下,您应该遍历结果集:

class SearchesController < ApplicationController
   def index
     # plural since we could get multiple users returned
     @users = User.search params[:search]
   end
end

and your view would be 你的看法是

<% @users.each do |u| %>
  <%= u.username %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM