如何编辑 HABTM 表中的附加数据?
如何更新 HABTM 表中的附加数据。
例如:我有 movies
、people
和 HABTM movies_people
表,但 中有额外的
表指示此人在该特定电影中的角色。 如何添加/更改该值?persontype_id
字段>movies_people
How do I update additional data in HABTM tables.
For Example: I have movies
, people
and HABTM movies_people
tables, but there is additional persontype_id
field in movies_people
table which indicates role of this person in that particular movie. How do I add/change this value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该使用 has_many :through 关联,它的引入正是出于这个原因。 您不仅有一个 movie_people 表,还拥有一个额外的模型,称为“Favorite”或“Viewing”之类的东西(取决于您试图通过关联完成的任务),它属于 :movies 和 :people,并且在您的 Movie 模型中,你会这样做
这使你可以访问 movie.people 来访问所有喜欢这部电影的人,以及 movie.favorites 来访问你的收藏夹表中的任何内容(例如 persontype_id)
Josh Susser 的文章,多对多舞蹈关闭在解释方面做得更好这是我所希望的,因此为了获得更多帮助,我会看一下它。 http://blog.hasmanythrough.com/2006/ 4/20/多对多舞蹈比赛
You should probably be using a has_many :through association, which was introduced just for this reason. Instead of just a movies_people table, you'd have an additional model, called somthing like Favorite or Viewing (depending on what you're trying to accomplish with the association), which belongs_to both :movies and :people, and in your Movie model, you'd do
This gives you access to movie.people for all the people who have favored this movie and movie.favorites to access anything that would be in your favorites table (like persontype_id)
Josh Susser's Article, Many-to-Many Dance Off does a much better job of explaining this that I could ever hope, so for additional help, I'd take a look at it. http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off
Ryan Bates 也发表了一篇关于该主题的精彩 Railscast:
http:// railscasts.com/episodes/47-two-many-to-many
There is also a great Railscast on the subject, by Ryan Bates:
http://railscasts.com/episodes/47-two-many-to-many