My question is more related to naming conventions than programmation I guess.
Let’s assume an application where users can create new articles (so they are the owner of these articles) and where you can add article “editors”, who can only update the article content.
class User
include Mongoid::Document
has_many :articles # as owner of the articles
has_and_belongs_to_many :articles # as editor of the articles
end
class Article
include Mongoid::Document
belongs_to :user
has_and_belongs_to_many :editors, :class_name => 'User'
end
What I want to know is how I should call the articles association in my User model. I mean, an article has an author and editors, which seems strong naming conventions to me, but a user has articles he created and articles he is the editor. How would you call/name/declare the last 2 associations?
I would call them as
:edited_articles, and:authored_articlesor:owned_articles, or something similarly straightforward names. Just dont forget to add the:class_nameand:foreign_keyor:throughqualifiers to them.Update:
For has_and_belongs_to_many relation you need a connection table, which is by default, is named of the two joined table. E.g.
articles_usersin your case. In this table you will propably have two ids,user_idandarticle_id. This way rails connects your models automatically.Of course if you call it
editor_idin the join table then use that. And the opposite, on the user side should work too.