I do not understand how to express the relationship between three separate tables

Given the following tables in ActiveRecord:

  • the authors
  • Sites
  • articles

I donโ€™t know how to express that the author receives a different amount depending on the publication, but the authors working in the same publication have different rates:

  • John publishes an article in Foo for $ 300
  • John publishes an article in Bar for $ 350
  • John publishes an article in the Base for $ 400.
  • Dick publishes $ 250 Foo article
  • Dick publishes an article in Bar for $ 400

and etc.

What connection am I trying to describe?

At the moment, I have a table of "bids" with author _id, site_id columns and quantity. Given publish.id and author.id, I get the article value using

cost = Rate.find(:first, :conditions => ["author_id = ? and site_id = ?", author.id, site.id]).rate

, , , , , "", "".

, , , - ", ... ", , Google.

+3
4

.

class Author
  has_many :publications, :through => :rates
end

class Publication
  has_many :authors, :through => :rates
end

class Rate #rich join table
  belongs_to :author
  belongs_to :publication
end

:

@author.rates.find_by_site_id(123)

,

@author.publications
@publication.authors
+5

, , .

, :

  • ( )
  • ( )
  • / ( )

:

  • (FK )
  • (FK )

. , , .

+1

, .

, , , . , "" . "--", "--" "--". .

"---", "----", ---- ..

n- .

, : : , . , . .

, :

PK AuthorID.
PK PublicationID.
PK ArticleID.

:

AuthorID (FK), PublicationID (FK), AtricleID (FK), , .

PK (AuthorID, PublicationID, ArticleID)

, . .

, , / . .

, . , , .

, Google, " ".

+1

:

author_site_mapper
------------------
id
author_id
site_id
rate

, "mapper". " " .

0

Source: https://habr.com/ru/post/1702853/


All Articles