0

我有什么(伪代码):轨道4 HABTM关系和额外的字段上的连接表

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

文件有用户(文件是批准,核准或没有),并在这方面加入表应为每个用户批准额外的列。

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. 

针对这一情况立即回答是可选的,会理解提供咨询架构太(也许我应该使用其他类型的关系?)。

+0

连接表的仅此而已。如果你想要连接对象的列,你需要查看[has_many through](http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association)关系。我的直觉告诉我,HABTM和has_many都不是这种情况的正确关系。我不知道我有足够的理解来提出更好的建议。 – 2015-02-10 03:57:24

回答

1

前段时间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 

我不明白合同如何适应这一点,我想你是说文件是一个合同?

我会把批准的方法在一个单独的类

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