class Comment < ActiveRecord::Base
belongs_to :users
belongs_to :articles
Wait.. what? A user has many articles through comments? Why, in the name
of Yehuda Katz's beard, would you want to do that?
A user has many articles
A user has many comments
An article has many comments
An article belongs to a user
A comment belongs to an article
A comment belongs to a user.
Well, in my model articles dont have a user association. They just
have comments and users can post comments to different articles
So basically article and user have a many to many relationship with
the comments being the join table (it contains both article_id and
user_id). The comments table also has an additional attribute, the
actual comment.
I guess the
has_many :articles, :through => :comments
reads funny. It could just as well been
has_many :articles, :through => :user_article
So, given this, when a new comment is being created, I would like to
be able to use a path like
user_article_comment_path([@user, @article])
What should I put in my routes file to get this?
I can do this
map.resources :users do |user|
user.resources :articles do |article|
article.resources :comments
end
end
but this gives me unnecessary paths like
user_article_path
which I dont want. I hope this makes sense.
Well, in my model articles dont have a user association. They just
have comments and users can post comments to different articles
Okay, so we have:
Article has many comments
Comment belongs to article
Comment belongs to user
User has many comments
So basically article and user have a many to many relationship with
the comments being the join table (it contains both article_id and
user_id). The comments table also has an additional attribute, the
actual comment.
*blink* Yes, but no. I mean.. Yes, but no! You're abusing the concept of
the many:many relationship. As long as you're aware of that, let's move
on...
So, given this, when a new comment is being created, I would like to
be able to use a path like
user_article_comment_path([@user, @article])
What should I put in my routes file to get this?
Well, I'm not all that good at routes yet, so can you tell me what you
expect this path to do for you?
*blink* Yes, but no. I mean.. Yes, but no! You're abusing the concept of
the many:many relationship. As long as you're aware of that, let's move
on...
Since the comments table has both the article and user id's it just
lends itself to be thought of that way, dont you think?
What else would you suggest given that
Okay, so we have:
Article has many comments
Comment belongs to article
Comment belongs to user
User has many comments
The user_article_comment_path([@user, @article])
will point to the "new" action in the comment controller and rails
knows to use user_id and article_id from the [@user, @article]
The user_article_comment_path([@user, @article])
will point to the "new" action in the comment controller and rails
knows to use user_id and article_id from the [@user, @article]
In other words, you want to create a new many-to-many relationship ?
Your problem is that one user can create several comments on the same
article, and that is not part of the many:many deal. I'm afraid you'll
need to go a more traditional route.