我正在开发一个Rails应用程序,这是艺术家们对其他艺术家所说的引用汇编,我无法弄清楚我的数据模型中的关系应该如何工作。
以此引用为例。以下是David Bowie关于Lou Reed所说的话:
“他是一位大师”
我在这个例子中的Quote模型的属性是这样的:
但是,如果Lou Reed对David Bowie说了些什么,比如说:
艺术家既可以是演讲者,也可以是报价的主题,但绝不是相同的引用。我还希望用户能够创建新的艺术家,然后在创建报价时将其显示为与报价关联的选项(通过下拉列表或搜索等)。
似乎我无法构建它以便引用有许多艺术家通过演讲者,因为它也可能有许多艺术家通过主题。
这是结构化的惯用Rails方法:
Quote
has_one :speaker
has_one :subject
Speaker
has_many :artists
belongs_to_many :quotes
Subject
has_many :artists
belongs_to_many :quotes
Artist
belongs_to_many :speakers
belongs_to_many :subjects
我相信你想要Quote看起来像:
class Quote < ActiveRecord::Base
belongs_to :speaker, class_name: "Artist"
belongs_to :subject, class_name: "Artist"
belongs_to :user
validates :speaker, uniqueness: {scope: :subject}
validates :subject, uniqueness: {scope: :speaker}
...
end
这假定:
这样,speaker
和subject
都被指定为类Artist
。
唯一性验证确保speaker
永远不会是subject
,反之亦然。 (你可能需要用validates
语句来调整,因为我没有测试过。)
在你的Artist
模型中,你可能想做类似的事情:
class Artist < ActiveRecord::Base
has_many :spoken_quotes, class_name: "Quote", foreign_key: :speaker_id
has_many :referencing_quotes, class_name: "Quote", foreign_key: :subject_id
...
end
这样,你可以这样做:
Artist.find_by(name: 'David Bowie').spoken_quotes
并得到所有Quotes
大卫鲍伊是speaker
。
而且,在你的User
模型中,你可能想要这样的东西:
class User < ActiveRecord::Base
has_many: :quotes
...
end
所以你可以这样做:
current_user.quotes
并获得current_user
创建的所有报价。
假设您有以下架构:
user - id
quote - author_id, subject_id
你可以像这样建立关系,利用这些关联方法可用的选项:
# User
has_many :sent_quotes, class_name: "Quote", foreign_key: :author_id
has_many :received_quotes, class_name: "Quote", foreign_key: :subject_id
has_many :quote_receivers, through: :sent_quotes, source: :subject
has_many :quote_senders, through: :received_quotes, source: :authors
# Quote
belongs_to :author, class_name: "User"
belongs_to :subject, class_name: "User"