many to many relationship with restful routes

Here is the scenario, Articles have many Comments Users can write many Comments for many Articles

The comments table contains both

user_id article_id

as foreign keys

My models are set up like so

class User < ActiveRecord::Base   has_many :comments   has_many :articles, :through => :comments

class Article < ActiveRecord::Base   has_many :comments   has_many :users, :through => :comments

class Comment < ActiveRecord::Base   belongs_to :users   belongs_to :articles

My routes.rb has the following code

  map.resources :articles, :has_many => :comments   map.resources :users, :has_many => :comments

which produces the following routes

new_article_comment edit_article_comment new_user_comment edit_user_comment etc...

This is not what I want (atleast not what I think I want), since comments must always be related to users and article, how can I get a route like so

new_user_article_comment edit_user_article_comment

Then I could just do

new_user_article_comment_path([@user, @article])

to create a new comment

Maulin pa wrote:

Here is the scenario, Articles have many Comments Users can write many Comments for many Articles

The comments table contains both

user_id article_id

as foreign keys

My models are set up like so

class User < ActiveRecord::Base   has_many :comments   has_many :articles, :through => :comments

class Article < ActiveRecord::Base   has_many :comments   has_many :users, :through => :comments

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.

Go from there, that's what makes sense.

Haha! The beard bit made me laugh :slight_smile:

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.

Maulin pa wrote:

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]

Maulin pa wrote:

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.