使用三个collection_selects进行动态过滤

时间:2012-08-17 13:09:40

标签: ruby-on-rails search filter

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

  1. 行业(用户现在工作的除了任何行业之外) 用户曾在此前工作过)
  2. 公司(用户工作的公司) 现在除了过去的任何一个)
  3. 学校(用户拥有的任何学校) 出席)。
  4. 这些属性正在从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

    不确定从哪里开始,可以真正使用一些帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我会考虑在不同的模型上使用3个属性表示。因此,您最终会拥有公司,行业和学校模型,用于存储用户过去和现在的公司,行业和学校条目。

这样,每个公司/学校/行业都可以拥有更多属性,例如用户在什么时间工作/上学。这样,一旦您使用User模型在3个模型之间建立模型关联,就像说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