【问题标题】:search nested_attributes in rails在 rails 中搜索 nested_attributes
【发布时间】:2013-05-29 09:17:11
【问题描述】:
class Subject
  has_many :subject_attribute_types
  has_many :subject_attributes

  accepts_nested_attributes_for :subject_attributes
end

class SubjectAttributeType
  belongs_to :subject
  has_many :subject_attributes

  attr_accessible :type_name
end

class SubjectAttribute
  belongs_to :subject
  belongs_to :subject_attribute_type

  attr_accessible :value
end

例如:

s1 = Subject.create()
s2 = Subject.create()

sat1 = SubjectAttributeType.create(subject: s1, name: 'Age')
sat2 = SubjectAttributeType.create(subject: s1, name: 'Sex')

sat3 = SubjectAttributeType.create(subject: s2, type_name: 'Age')
sat5 = SubjectAttributeType.create(subject: s2, type_name: 'Username')

SubjectAttribute.create(subject: s1, subject_attribute_type: sat1, value: 20)
SubjectAttribute.create(subject: s1, subject_attribute_type: sat2, value: "male")
SubjectAttribute.create(subject: s2, subject_attribute_type: sat3, value: 21)
SubjectAttribute.create(subject: s2, subject_attribute_type: sat1, value: "user1")

问题:
对确切的 subject_attributes 进行搜索的最佳做法是什么。
如果我想查找所有年龄 >= 18 且昵称如 %user%

的主题

目前我正在使用 ransack gem,但我不知道如何在nested_attributes 上进行搜索

【问题讨论】:

    标签: ruby-on-rails search nested-attributes ransack


    【解决方案1】:

    我发现您的应用的业务逻辑存在问题。为什么需要您的 AttributeType 来了解任何主题?

    class Subject < ActiveRecord::Base
      has_many :subject_attributes
      has_many :attribute_types, through: :subject_attributes
    end
    
    class SubjectAttribute < ActiveRecord::Base
      belongs_to :attribute_type
      belongs_to :subject
      attr_accessible :attribute_type_id, :subject_id, :value
    end
    
    class AttributeType < ActiveRecord::Base
      attr_accessible :type_name
    end
    

    之后如果你插入一些数据:

    s1 = Subject.create
    s2 = Subject.create
    
    sat1 = AttributeType.create(type_name: "Age")
    sat2 = AttributeType.create(type_name: "Sex")
    sat3 = AttributeType.create(type_name: "Username")
    
    SubjectAttribute.create(subject:s1, attribute_type:sat1, value: 20)
    SubjectAttribute.create(subject:s1, attribute_type:sat2, value:"male")
    SubjectAttribute.create(subject:s2, attribute_type:sat1, value:21)
    SubjectAttribute.create(subject:s2, attribute_type:sat3, value:"user1")
    

    您将能够进行选择。 在您的示例中,您使用了多个属性,因此您必须提出多个请求:

    这样你会找到带有值名称的主题:

    names = Subject.joins(:attribute_types).where("attribute_types.type_name = 'Username'   
                                                   and value like '%user%'")
    => [#<Subject id: 2, created_at: "2013-05-29 11:11:51", updated_at: "2013-05-29 11:11:51">]
    

    这样你会找到价值年龄的主题

    ages = Subject.joins(:attribute_types).where("attribute_types.type_name = 'Age' 
                                                  and value >= 18")
    => [#<Subject id: 1, created_at: "2013-05-29 11:11:42", updated_at: "2013-05-29 11:11:42">, 
       #<Subject id: 2, created_at: "2013-05-29 11:11:51", updated_at: "2013-05-29 11:11:51">]
    

    这样你会找到相交的主题

    subjects = (names&ages)
    => [#<Subject id: 2, created_at: "2013-05-29 11:11:51", updated_at: "2013-05-29 11:11:51">] 
    

    使用动态属性类型使选择变得非常困难。因此,如果您可以对每个类型值参数进行单独请求,请使用它。否则也许它真的只是主题列?

    【讨论】:

    • 1) 业务逻辑。是的!您是绝对正确的,在主题关联中不需要 AttributeType。 2) 否则可能它真的只是主题列? 不是解决方案,因为将来 mby 将添加新的 AttributeType。例如“体重或身高”。另外,并非所有的 AttributeTypes 都在每个主题中使用。 3)几个请求是的,这是一个解决方案,但给出了同样的问题,即使用新的 AttributeType,还需要添加请求。
    • 您仍然可以即时执行此操作:foreach 对请求然后将它们与 & 相交。如果你只需要所有属性来过滤你的主题,那就是这样。
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多