Given the following tables in ActiveRecord:
- authors
- sites
- articles
I don’t know how to express that an author is paid a different amount depending on the publication, but that authors working for 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 Baz for $400
- Dick publishes an article in Foo for $250
- Dick publishes an article in Bar for $400
etc.
What kind of relationship am I trying to describe?
At the moment I’ve got a ‘rates’ table with author_id, site_id and amount columns. Given publication.id and author.id, I derive the cost of the article with
cost = Rate.find(:first, :conditions => ['author_id = ? and site_id = ?', author.id, site.id]).rate
That works, but I’m not sure it’s the best way, and I’m not sure how to make sure I don’t end up with ‘John’ having two rates for ‘Baz.’
I don’t think I want code so much as I want someone to say ‘Oh, that’s a … relationship’ so I can get a grip on what I’m Googleing for.
Its a has and belongs to many with a rich join table.
And you can then simplify your finding like this:
Plus you get direct access accross the join table