2016-09-01 21 views
0

以下面的示例爲基礎...並且如果設置不正確,請糾正我的錯誤。如何在has-many:through關係中的特定字段上執行太陽黑子搜索

遷移文件:

class CreateUsers < ActiveRecord::Migration 
    def change 
    create_table :users do |t| 
     t.string :first_name, null: false 
     t.string :last_name, null: false 
     t.string :email, null: false 

     t.timestamps null: false 
    end 
    end 
end 

User.create(first_name:'John', last_name:'Smith', email:'[email protected]') # id: 1 
User.create(first_name:'Jane', last_name:'Smith', email:'[email protected]') # id: 2 

class CreateCustomAttributes < ActiveRecord::Migration 
    def change 
    create_table :custom_attributes do |t| 
     t.string :name, null: false 
     t.text :description, null: true 

     t.timestamps null: false 
    end 
    end 
end 

CustomAttribute.create(name:'is_married', description:'') # id: 1 
CustomAttribute.create(name:'has_children', description:'') # id: 2 
CustomAttribute.create(name:'number_of_children', description:'') # id: 3 

class CreateUserCustomAttributes < ActiveRecord::Migration 
    def change 
    create_table :user_custom_attributes do |t| 
     t.references :user, null: false 
     t.references :custom_attribute, null: false 
     t.text  :value, null: true 

     t.timestamps null: false 
    end 
    end 
end 

# John 
UserCustomAttribute.create(user_id: 1, custom_attribute_id: 1, value: 'no') 
UserCustomAttribute.create(user_id: 1, custom_attribute_id: 2, value: 'yes') 
UserCustomAttribute.create(user_id: 1, custom_attribute_id: 3, value: 4) 

# Jane 
UserCustomAttribute.create(user_id: 2, custom_attribute_id: 1, value: 'no') 
UserCustomAttribute.create(user_id: 2, custom_attribute_id: 2, value: 'no') 
UserCustomAttribute.create(user_id: 2, custom_attribute_id: 3, value: 0) 

型號:

class CustomAttribute < ActiveRecord::Base 
    has_many :user_custom_attributes, :dependent => :destroy 
    has_many :users, through: :user_custom_attributes 
end 

class UserCustomAttribute < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :custom_attribute 
end 

class User < ActiveRecord::Base 
    has_many :user_custom_attributes, :dependent => :destroy 
    has_many :custom_attributes, through: :user_custom_attributes 

    searchable do 
    text :first_name, :last_name, :email 
    end 
end 

我試圖讓 「價值」 是動態的,無論是簡單的布爾(0,1),字符串( ''快速')或序列化的項目('----棒球 - 足球')

是否可以搜索所有有子女的用戶(例如'has_children'設置爲'yes')?

例如:?/用戶FQ [has_children] =是

另外,將能夠搜索與兒童的所有用戶(例如 'NUMBER_OF_CHILDREN' 是大於0)?

如果是這樣,你將如何構建用戶模型和User.search塊中的「可搜索」塊?

回答

0

最簡單的做法是首先分離出你想要的不同類型的動態字段(字符串,整數,布爾值),也許通過添加一個attribute_type:'string',attribute_type:'numerical 「等等,那麼你可以使用太陽黑子的動態字段進行搜索不同類型的動態數據:

searchable do 
    dynamic_string :string_attributes do 
    user_custom_string_attributes.inject({}) do |hash, uca| 
     hash.merge(uca.custom_attribute.name.to_sym => uca.value) 
    end 
    end 

    dynamic_integer :numerical_attributes do 
    user_custom_numerical_attributes.inject({}) do |hash, nuca| 
     hash.merge(nuca.custom_attribute.name.to_sym => nuca.value.to_i) # just making sure it's an integer! 
    end 
    end 
end 

# Separate out the different types of attributes for different dynamic blocks 
# via methods like the one below. 

def user_custom_numerical_attributes 
    user_custom_attributes.where(attribute_type: 'numerical') 
end 

def user_custom_string_attributes 
    user_custom_attributes.where(attribute_type: 'string') 
end 

這會給你搜索‘string_attributes’看起來像對象{:has_children =>‘不’}或」 numerical_attributes '就像{:number_of_children => 2}。

然後你可以用塊搜尋,像這樣:

User.search do 
    dynamic :string_attributes do 
    with(:has_children, 'no') 
    end 

    dynamic :numerical_attributes do 
    with(:number_of_children).greater_than(0) 
    end 
end 

您可以創建不同的文本幾乎任何類型的數據動態字段;爲此,你必須使用字符串。在dynamic :field_name do區塊內設置動態字段後,您可以使用Sunspot readme中示例中演示的任何非文本搜索方法。