如何编辑 HABTM 表中的附加数据?

发布于 2024-07-11 19:02:59 字数 218 浏览 6 评论 0原文

如何更新 HABTM 表中的附加数据。

例如:我有 moviespeople 和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-07-18 19:02:59

您可能应该使用 has_many :through 关联,它的引入正是出于这个原因。 您不仅有一个 movie_people 表,还拥有一个额外的模型,称为“Favorite”或“Viewing”之类的东西(取决于您试图通过关联完成的任务),它属于 :movies 和 :people,并且在您的 Movie 模型中,你会这样做

has_many :favorites
has_many :people, :through => :favorites

这使你可以访问 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

has_many :favorites
has_many :people, :through => :favorites

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

郁金香雨 2024-07-18 19:02:59

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文