如何在模型中嵌套模型

发布于 2024-11-24 15:26:26 字数 494 浏览 1 评论 0原文

想象一下我有两个模型

Film
-name
-description
-duration
-year_made
-rating
-actors

Actor
-name
-d_o_b
-biography
-films

演员嵌套在电影中,反之亦然。

如何在 Ruby 模型中表示这种关系?实际上,我会有第三个表将 actor_idfilm_id 进行映射。

在向电影添加细节时,我希望能够动态创建一个演员(如果演员不存在,请使用提供的名称创建一个新演员)

提前谢谢您。

添加

刚刚找到了类似问题

Imagine I have two models

Film
-name
-description
-duration
-year_made
-rating
-actors

Actor
-name
-d_o_b
-biography
-films

Actors are nested in a Film and vice versa.

How do I represent this relationship in my Ruby models? Realistically I would have a third table mapping actor_id with film_id.

Whilst adding details to a film I would like to be able to create an actor on the fly(if an actor does not exist create a new one with the name supplied)

Thank you in advance.

ADDITION:

Just found a link to a similar question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

花落人断肠 2024-12-01 15:26:27

您正在查看两个表之间的“拥有”和“属于多个”(HABTM) 关系。
在 Rails 指南中阅读有关 HABTM 关系的信息:http://edgeguides.rubyonrails.org /association_basics.html#has_and_belongs_to_many-association-reference

首先你需要生成一个迁移,如下所示:

class AddActorFilmTable < ActiveRecord::Migration
  def self.up
    create_table :actors_films, :id => false  do |t|
      t.integer :actor_id, :null => :false
      t.integer :film_id, :null => :false
    end
    add_index :actors_films, [:actor_id, :film_id], :unique => true
  end

  def self.down
    drop_table :actors_films
  end
end

然后在模型中指定:

class Actor < ActiveRecord::Base
  has_and_belongs_to_many :films
end

class Film < ActiveRecord::Base
  has_and_belongs_to_many :actors
end

这将允许您为这种类型的关系使用所有附加的 Rails 方法。要在表单中使用它,您可以遵循 RailsCast 17:HABTM 复选框 - 尽管它很旧,它应该仍然适用。或者,您可以使用 Simple Form 之类的 gem 轻松生成关联,如下所示:

form_for @actor do |f|
    f.collection_check_boxes :film_ids, Film.all, :id, :name
end

You're looking at a Has and Belongs to Many (HABTM) relationship between the two tables.
Read about HABTM relationship in the Rails guides here: http://edgeguides.rubyonrails.org/association_basics.html#has_and_belongs_to_many-association-reference

First you'll need to generate a migration which will look something like this:

class AddActorFilmTable < ActiveRecord::Migration
  def self.up
    create_table :actors_films, :id => false  do |t|
      t.integer :actor_id, :null => :false
      t.integer :film_id, :null => :false
    end
    add_index :actors_films, [:actor_id, :film_id], :unique => true
  end

  def self.down
    drop_table :actors_films
  end
end

and then specify in your models:

class Actor < ActiveRecord::Base
  has_and_belongs_to_many :films
end

class Film < ActiveRecord::Base
  has_and_belongs_to_many :actors
end

This will allow you to use all of the additional Rails methods for this type of relationship. To use this in a form, you could follow RailsCast 17: HABTM Checkboxes - though it's old, it should still apply. Alternatively, you can use a gem like Simple Form to easily generate the associations for you like so:

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