以下面的示例爲基礎...並且如果設置不正確,請糾正我的錯誤。如何在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塊中的「可搜索」塊?