rails 4 个 HABTM 关系和加入时的额外字段 table

rails 4 HABTM relation and extra fields on join table

我有什么(伪代码):

model Document
    column :title
    HABTM  :users
model User 
    column :name
    HABTM  :documents

文档有用户(是文档的批准者,批准或不批准),在这种情况下,加入 table 应该为每个用户批准额外的列。

jointable
    user_id, document_id, approved
    1      , 1          ,    true
    2      , 1          ,    false

我想要的基本上是:

contract.approvers => returns users but with possibility to =>
contract.approvers.first.approve(:true) => and it updates JOINtable approve column to TRUE.

这种情况的正确答案是可选的,也将不胜感激关于模式的建议(或者我应该使用其他类型的关系?)。

HABTM 已经被弃用了一段时间,我认为它只是一个参考,现在已经有很多了。

无论如何

join table name = DocumentReview

Document 
  has_many :document_reviews
  has_many :users, through: :document_reviews

User
  has_many :document_reviews
  has_many :documents, through: :document_reviews

我不明白合同如何适用于此,我想你是说文件就是合同?

我会把 approve 方法放在一个单独的 class

class DocumentSignOff

  def initialize(user, document)
    @document_review = DocumentReview.find_by(user: user,document: document)
  end 

  def approve!
    #maybe more logic and such
    @document_review.udpate(approved: true)
  end
end

结束