我怎么参考,并改变了一个额外的价值有许多通过模型 [英] how do I reference and change an extra value in a has many through model
问题描述
我得piece_lineup模型加入阵容和连身款(有许多通过)。我添加了一个状态列到piece_lineup模型,但我无法弄清楚如何引用属性和/或改变它。当列出了阵容相关的作品,我也希望,因为它涉及到阵容列出片状态。我该怎么办呢?
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?
推荐答案
这很简单,让此列。添加到您的模型:
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"
如果您需要更改此值,则应该创建阵容#status_for_piece =(片,状态)
(为例)的方法。查找所需的行和更新状态。
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
这篇关于我怎么参考,并改变了一个额外的价值有许多通过模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!