如何对“特色”的概念进行建模? (即,当某位艺术家出现在歌曲中时)

发布于 2024-08-05 10:15:42 字数 641 浏览 5 评论 0原文

有时,一首歌会有不止一位艺术家参与。例如,Jay-z 的新歌“A Star is Born”以艺术家 Cole 为主角,因此在目录中将被列为“Jay-z (featuring Cole) - A Star is Born”。我的问题是如何在我的数据库中对此进行建模。

现在非常简单:每首歌曲belongs_to :artist并且每个艺术家has_many :songs。我想改变这一点,以便歌曲有许多艺术家,其中一位艺术家被指定为“主要”——即,如果 A、B 和 C 都与给定歌曲相关联,并且 A 是主要的,则该歌曲的艺术家将是显示为“A(包含 B 和 C)”。

这是我的想法:

歌曲:

has_many :artists, :through => :performances

艺术家:

has_many :songs, :through => :performances

其中 performance 模型将有一个 primary? 字段,指定给定的表演是否是“主要”表演歌曲,这意味着与该表演相关的艺术家是该歌曲的主要艺术家。

这种方法有意义吗?

Sometimes more than one artist will be on a song. For example, Jay-z's new song "A Star is Born" features the artist Cole, and thus would be listed as "Jay-z (featuring Cole) - A Star is Born" in a catalog. My question is how to model this in my database.

Right now it's pretty simple: every song belongs_to :artist and every artist has_many :songs. I want to change this so that songs have many artists, with one artist specified as "primary" -- i.e., if A, B, and C are all associated with a given song and A is primary, the artist of that song will be displayed as "A (featuring B and C)".

Here's what I'm thinking:

Song:

has_many :artists, :through => :performances

Artist:

has_many :songs, :through => :performances

where the performance model would have a single field for primary?, designating whether a given performance was the "primary" performance on the song, meaning that the artist associated with that performance is the primary artist on the song.

Does this approach make sense?

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

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

发布评论

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

评论(2

瑾兮 2024-08-12 10:15:42

是的。你成功了。

Yes. You nailed it.

幽梦紫曦~ 2024-08-12 10:15:42

看起来您正在使用表演作为连接表,并添加了将歌曲与艺术家链接起来的信息。但这效果还不够好。歌曲可以有多次表演,每场表演可以有多个表演者。

我将重组数据库以使用以下结构。不幸的是,将歌曲添加到数据库需要更多的工作,但允许翻唱、歌曲的多个版本和二重唱。

歌曲(属性:标题)

  • 有许多表演
  • 有许多表演者(通过表演)
  • 有许多艺术家(通过表演者)

表演(外键:Song_id,属性:日期、年份、长度、流派、专辑,)

  • 属于歌曲
  • 有许多表演者
  • 有许多艺术家(通过表演者)

表演者(外键:Performance_id,Artist_id,属性:角色)

  • 有一首歌曲(通过表演者)注意 has_one :through 不是 Rails 的核心功能
  • 属于 Performances
  • 属于 Artist

艺术家(属性:姓名、出生日期等(其他个人信息))

  • 有许多 Performers
  • 有许多表演(通过表演者)
  • 有许多歌曲(通过表演)

如果您想强制执行 1 个主要艺术家/表演,则可以在表演者中进行验证。

class Performers < ActiveRecord::Base
  validates_uniqueness_of :role, :scope => [:performance_id], :if => Proc.new{|performer| performer.role == "primary"}
end

It looks like you're using performances as an join table with added information to link songs to artists. But this does not work well enough. Songs can have many performances, and each performance can have multiple performers.

I would restructure the database to use a the following structure. Unfortunately it would require a little more work to add a Song to the database, but allows for covers, multiple versions of the song, and duets.

Song (Attributes: Title)

  • has many Performances
  • has many Performers (through Performances)
  • has many Artists (through Performers)

Performance (Foreign Key: Song_id, Attributes: Date, Year, Length, Genre, Album, )

  • belongs to Song
  • has many Performers
  • has many Artist (through Performers)

Performers (Foreign keys: Performance_id, Artist_id, Attributes: Role)

  • has one Song (through Performers) N.B. has_one :through is not a core feature of Rails
  • belongs to Performances
  • belongs to Artist

Artist (Attributes: Name, Date of Birth, etc. (other Personal inforamtion))

  • has many Performers
  • has many Performances (through Performers)
  • has many Songs (through Performances)

If you wanted to enforce 1 primary artist/performance it would be done as validation in Performers.

class Performers < ActiveRecord::Base
  validates_uniqueness_of :role, :scope => [:performance_id], :if => Proc.new{|performer| performer.role == "primary"}
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文