how do I reference and change an extra value in a has many through model

谁说我不能喝 提交于 2019-12-08 11:19:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!