旧数据库的 ActiveRecord 连接表

发布于 2024-07-30 00:55:42 字数 575 浏览 5 评论 0原文

我有一个遗留数据库,正在努力让 ActiveRecord 能够使用它。 我遇到了连接表的问题。 我有以下内容:

class TvShow < ActiveRecord::Base

  set_table_name "tvshow"
  set_primary_key "idShow"

end

class Episode < ActiveRecord::Base
  set_table_name "episode"
  set_primary_key "idEpisode"
end

然后我有一个名为 tvshowlinkepisode 的表,它有 2 个字段: idShow、idEpisode 所以我有 2 个表和它们之间的联接(因此是多对多关系),但是联接使用非标准外键。 我的第一个想法是创建一个名为 TvShowEpisodeLink 的模型,但没有主键。 这个想法是,由于外键是非标准的,我可以使用 set_foreign_key 并进行一些控制。 最后我想说一些类似 TvShow.find(:last).episodes 或 Episode.find(:last).tv_show 的内容。 我如何到达那里?

I have a legacy database that I'm working on getting ActiveRecord to work with. I've run into a problem with join tables. I have the following:

class TvShow < ActiveRecord::Base

  set_table_name "tvshow"
  set_primary_key "idShow"

end

class Episode < ActiveRecord::Base
  set_table_name "episode"
  set_primary_key "idEpisode"
end

And then I have a table called tvshowlinkepisode that has 2 fields: idShow, idEpisode So I have 2 tables and a join between them (so a many to many relationship), however the join uses non-standard foreign keys. My first thought was to create a model called TvShowEpisodeLink but there isn't a primary key. The idea was that since the foreign keys are non-standard I could use the set_foreign_key and have some control. Ultimately I want to say something like TvShow.find(:last).episodes or Episode.find(:last).tv_show. How do I get there?

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

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

发布评论

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

评论(4

寂寞美少年 2024-08-06 00:55:42

我相信您可以使用 has_and_belongs_to_many 选项比 Alvaro 的答案稍微优雅一些​​,尽管他的答案非常好,并且将为您班级的任何客户带来相当相同的功能。

class TvShow < ActiveRecord::Base

  set_table_name "tvshow"
  set_primary_key "idShow"
  has_and_belong_to_many :episodes, 
                         :join_table => "tvshowlinkepisode", 
                         :foreign_key => "idShow",
                         :association_foreign_key => "idEpisode"

end

class Episode < ActiveRecord::Base
  set_table_name "episode"
  set_primary_key "idEpisode"
  has_and_belongs_to_many :tv_shows,
                          :join_table => "tvshowlinkepisode",
                          :foreign_key => "idEpisode",
                          :association_foreign_key => "idShow"
end

请注意,:foreign_key 选项指定哪一列是链接“这一侧”的类的 id,而 :association_foreign_key 指定哪一列是链接“另一侧”的类的 id。

与阿尔瓦罗的答案相比,这种模式应该避免实例化任何不必要的对象来表示链接。

I believe you can be slightly more elegant than Alvaro's answer using options to has_and_belongs_to_many, though his answer is perfectly fine and will result in fairly identical functionality for any clients of your class.

class TvShow < ActiveRecord::Base

  set_table_name "tvshow"
  set_primary_key "idShow"
  has_and_belong_to_many :episodes, 
                         :join_table => "tvshowlinkepisode", 
                         :foreign_key => "idShow",
                         :association_foreign_key => "idEpisode"

end

class Episode < ActiveRecord::Base
  set_table_name "episode"
  set_primary_key "idEpisode"
  has_and_belongs_to_many :tv_shows,
                          :join_table => "tvshowlinkepisode",
                          :foreign_key => "idEpisode",
                          :association_foreign_key => "idShow"
end

Note that the :foreign_key option specifies which column is the id for the class on "this side" of the link, while :association_foreign_key specifies the column that is the id for the class on the "other side" of the link.

Contrast this with Alvaro's answer, this pattern should avoid instantiation of any unnecessary objects to represent the link.

撩起发的微风 2024-08-06 00:55:42

这项工作为你...

class TvShow < ActiveRecord::Base
  set_table_name "tvshow"
  set_primary_key "idShow"

  has_many :tv_show_link_episode, :foreign_key => 'idShow'
  has_many :episodes, :through => :tv_show_link_episode
end


class Episode < ActiveRecord::Base
  set_table_name "episode"
  set_primary_key "idEpisode"

  has_many :tv_show_link_episode, :foreign_key => 'idEpisode'
  has_many :tv_shows, :through => :tv_show_link_episode

end

class TvShowLinkEpisode  < ActiveRecord::Base
  set_table_name "tvshowlinkepisode"

    # the foreign key is named by the TvShowLinkEpisode field, 
    # the primary key name is for the primary key of the associated class
    belongs_to :tv_show, :foreign_key => 'idShow'
    belongs_to :episode, :foreign_key => 'idEpisode'
end

This work for you...

class TvShow < ActiveRecord::Base
  set_table_name "tvshow"
  set_primary_key "idShow"

  has_many :tv_show_link_episode, :foreign_key => 'idShow'
  has_many :episodes, :through => :tv_show_link_episode
end


class Episode < ActiveRecord::Base
  set_table_name "episode"
  set_primary_key "idEpisode"

  has_many :tv_show_link_episode, :foreign_key => 'idEpisode'
  has_many :tv_shows, :through => :tv_show_link_episode

end

class TvShowLinkEpisode  < ActiveRecord::Base
  set_table_name "tvshowlinkepisode"

    # the foreign key is named by the TvShowLinkEpisode field, 
    # the primary key name is for the primary key of the associated class
    belongs_to :tv_show, :foreign_key => 'idShow'
    belongs_to :episode, :foreign_key => 'idEpisode'
end
秋千易 2024-08-06 00:55:42

该关系是一对多的,因此您需要在表中使用belongs_to/has__many 关系。 如果您的数据库有视图,您可以通过表视图屏蔽非标准外键。

不确定这是否 100% 符合您的需求,但我希望它至少能给您一个想法。

The relationship is one-to-many so you need to use the belongs_to/has__many relationship in the tables. If your database has views, you could mask the non-standard foreign keys by a view for the tables.

Not sure if that fits 100% what you need, but I hope it at least gives you an idea.

小嗷兮 2024-08-06 00:55:42

有了这个,你不需要设置表视图,实际上,表视图它不是“Rails Way”,

试试这个:

>> TvShow.find(1).episodes 
#=> returns an array with [#<Episode idEpisode: 1, test: "Episode 1">]

>> Episode.find(1). tv_shows 
#=> returns an array with [#<TvShow idShow: 1, test: "tvshow 1">]

然后你可以做一些类似的事情:

e = Episode.find(1)
TvShow.find(1). episodes << e
#=> this make the proper association

With this, you don't need to set table views, actually, tables views it's not "The Rails Way",

Try this:

>> TvShow.find(1).episodes 
#=> returns an array with [#<Episode idEpisode: 1, test: "Episode 1">]

>> Episode.find(1). tv_shows 
#=> returns an array with [#<TvShow idShow: 1, test: "tvshow 1">]

Then you can do some stuff like:

e = Episode.find(1)
TvShow.find(1). episodes << e
#=> this make the proper association
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文