0

What I have (pseudo code):

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

Document has users (being approvers for document, either approve or not), and in this context join table should have extra column approved for each user.

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

What I want is basically:

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

Answer right for this situation is optional, will appreciate advises on schema too (or maybe i should use other type of relation?).

user229044
  • 232,980
  • 40
  • 330
  • 338
ClassyPimp
  • 715
  • 7
  • 20
  • A join table is only that. If you want columns on the object that joins you'll need to look into the [has_many through](http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association) relation. My gut tells me neither HABTM nor has_many through are the right relation for this situation. I don't know that I understand enough to suggest something better. – rb- Feb 10 '15 at 03:57

1 Answers1

1

HABTM has been deprecated a while ago, I think it is just a reference to has many through now.

Either way

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

I don't understand how contract fits into this, i think you are saying that a document is a contract?

I would put the approve method in a separate 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

end

Austio
  • 5,939
  • 20
  • 34