0

我网站上的每个用户都有一个列表,其中包含不同类型的用户。我正在使用 has_many through 关系来执行此操作,如下所示:
List.rb:

class List < ActiveRecord::Base
  belongs_to :company
  has_many :list_applicants
  has_many :applicants, through: :list_applicants
end

申请人.rb:

class Applicant < ActiveRecord::Base
  has_many :list_applicants
  has_many :lists, through: :list_applicants
end

ListApplicant.rb

class ListApplicant < ActiveRecord::Base
  attr_accessible :applicant_id, :list_id
  belongs_to :applicant
  belongs_to :list
end

公司.rb:

 class Company < ActiveRecord::Base
   has_one :list
 end

当用户将另一个用户添加到他们的列表中时,我想记录添加用户的日期,以便他们可以按添加日期对用户列表进行排序。做这个的最好方式是什么?

4

1 回答 1

1

您可以使用模型的created_at字段(ListApplicant如果有)。如果没有,您可以手动添加一个类似的字段。

更新:

您可以通过指定申请人和列表来访问该字段,如下所示:

@applicant.list_applicants.where(list_id: @list.id).first.created_at
于 2012-11-27T08:23:53.303 回答