问题
I have Lineup and Piece models joined by piece_lineup model (has many through). I added a 'status' column to the piece_lineup model but i can't figure out how to reference that attribute and/or change it. When listing the pieces associated with a lineup, I also want to list the status of the piece as it relates to the lineup. How do I do that?
回答1:
It's very simple to get this column. Add to your model:
has_many :pieces, through: :piece_lineup, select: "pieces.*, piece_lineup.status as status"
If you have to change this value, you should create Lineup#status_for_piece=(piece, status)
(as example) method. Find necessary row and update status.
Inserting is not elegant way, but join table is not a good place for frequently updated data at all
来源:https://stackoverflow.com/questions/11180025/how-do-i-reference-and-change-an-extra-value-in-a-has-many-through-model