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