使用连接模型为多对多关联的一侧设置类型

发布于 2024-10-04 16:30:13 字数 561 浏览 0 评论 0原文

我设置了一个公共 github 应用程序(请参阅:https://github.com/greenplastik/testapp 下载)来解决我在通过连接模型在两个模型之间的多对多关联的一侧指定类型时遇到的问题。

给定 Person 和 Book 模型以及 Book_Person 连接模型,我希望能够执行以下操作:

@book = Book.first @book.people # 列出书籍的人员 @book.authors # 列出书籍的作者类型人员 @book.editors # 列出书籍的编辑类型人员

@person = Person.first @person.books # 列出供人们阅读的书籍

此应用程序的部分设置是使用通过 Google 找到的说明。我的测试应用程序的自述文件中有指向这些说明的链接。

我尽力消除不一致和拼写错误。我无法让它工作。

任何帮助将不胜感激。我已经包含了 sqlite 数据库,以便您更轻松地进行测试。

I set up a public github app (see: https://github.com/greenplastik/testapp to download) to work through a problem I'm having with specifying a type on one side of a many-to-many association between two models, via a join model.

Given Person and Book models and a Book_Person join model, I want to be able to do the following:

@book = Book.first
@book.people # lists people for book
@book.authors # lists author-type people for book
@book.editors # lists editor-type people for book

and

@person = Person.first
@person.books # lists books for people

This app was set up in part using the instructions found through Google. There's a link to those instructions in the README of my testapp.

I tried, as best I could, to remove the inconsistencies and typos. I can't get it to work.

Any help would be appreciated. I've included the sqlite database for easier testing on your end.

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

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

发布评论

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

评论(1

谁许谁一生繁华 2024-10-11 16:30:13

很抱歉,我没有时间发布完整的解决方案或经过测试的解决方案,但这至少应该让您走上正确的轨道。

实现您正在寻找的内容的最佳方法是使用单表继承与多态关系一起使用。

我建议将 Author 和 Editor 重新定义为 Person 的子类,

但是

class Person < ActiveRecord::Base
  has_many :book_people
  has_many :books, :through => :book_people
end


class Author < Person
end

class Editor < Person
end

class BookPerson < ActiveRecord::Base
  belongs_to :person, :polymorphic => true
  belongs_to :book
end

class Book < ActiveRecord::Base
  has_many :book_people
  has_many :people, :through => :book_people
  has_many :authors, :through => :book_people, :source => :person, :source_type => "Author"
  has_many :editors, :through => :book_people, :source => :person, :source_type => "Editor"
end

,如果一个人可以担任所有三个角色,那么这将是次优的。可能有一种方法可以解决这个问题,即为它们三个使用相同的 STI 名称。但这样一来,查询所有作者就会变得更加困难。

I'm sorry i haven't got time to post neither a full solution or a tested one, but this should at least put you on the right track..

The best way to achieve what you're looking for is with Single Table Inheritance used with a polymorphic relationship.

I'd suggest redefining Author and Editor to be subclasses of Person

As in

class Person < ActiveRecord::Base
  has_many :book_people
  has_many :books, :through => :book_people
end


class Author < Person
end

class Editor < Person
end

class BookPerson < ActiveRecord::Base
  belongs_to :person, :polymorphic => true
  belongs_to :book
end

class Book < ActiveRecord::Base
  has_many :book_people
  has_many :people, :through => :book_people
  has_many :authors, :through => :book_people, :source => :person, :source_type => "Author"
  has_many :editors, :through => :book_people, :source => :person, :source_type => "Editor"
end

However this would be suboptimal if a single person could fill all three roles. There's probably a way around that by using the same STI name for the three of them. But then you'd make it harder to query for all authors.

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