【问题标题】:dynamic filtering with three collection_selects使用三个 collection_selects 进行动态过滤
【发布时间】:2012-08-13 22:38:06
【问题描述】:

我有一个所有用户的列表,我希望其他用户能够根据用户的三个总括属性进行过滤:

  1. 行业(用户现在工作的行业,除 用户以前工作过)
  2. 公司(用户工作的公司) 现在除了过去的那些)
  3. 学校(用户拥有的任何 参加)。

这些属性是从 LinkedIn 中提取的,因此每个行业/公司/学校在我的用户模型中都是自己的数据字段(当前/过去一个/过去两个/等)。

这是我的架构的一部分,用于展示我的用户模型:

create_table "users", :force => true do |t|
    ...
    t.string   "first_name"
    t.string   "last_name"
    ...
    t.string   "current_company"
    t.string   "past_company_one"
    t.string   "past_company_three"
    ...
    t.string   "current_industry"
    t.string   "past_industry_one"
    t.string   "past_industry_two"
    t.string   "past_industry_three"
    ...
    t.string   "school_zero_name"
    t.string   "school_one_name"
   ...
end

我想要三个 collection_select 字段(行业/公司/学校),用户可以使用它们来动态地过滤返回给他们的用户列表。它的外观和功能与此处的三个选择/过滤字段非常相似:https://angel.co/startups

不确定从哪里开始,确实需要一些帮助。谢谢。

【问题讨论】:

    标签: ruby-on-rails search filter


    【解决方案1】:

    我会考虑在单独的模型上使用 3 个属性表示。因此,您最终将拥有 Company、Industry 和 School 模型来存储用户过去和当前的公司、行业和学校条目。

    这样,每个公司/学校/行业都可以拥有更多属性,例如用户工作/上学的时间段。有了这个,一旦你在 3 个模型和 User 模型之间建立了模型关联,就很容易说 user.companies 或 user.schools。您还可以在公司和行业模型中添加一个字段,以指示用户当前所在的公司或行业,以区分用户过去工作过的公司/行业。

    类似这样的:

    class User < AR::Base
      has_many :companies
      has_one  :current_company, :class_name => "Company", :conditions => { :current => true }
    end
    
    class Company < AR::Base
      # == Schema attributes
      #
      #  id               :integer(4)      not null, primary key
      #  name             :integer(4)
      #  user_id          :integer(4)
      #  current          :boolean         :default => false, :null => false
      #  start_date       :date
      #  end_date         :date
    
      belongs_to :user
    end
    

    【讨论】:

    • 谢谢。你的意思是belongs_to :user 而不是belongs_to :company?
    • 另外,一旦设置了这些新模型,我仍然需要设置动态过滤(按公司 y 过滤行业 x 中返回的一组用户)。希望有人指出任何好的资源
    • 那是一个错字,我更新了答案。对于过滤器,请尝试使用命名范围。 Ryan Bates 有a nice screencast 讨论这个话题。祝你好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2019-01-30
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多