Note that there are some explanatory texts on larger screens.

plurals
  1. POrails map.resources with has_many :through doesn't work?
    primarykey
    data
    text
    <p>I've got three (relevant) models, specified like this:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :posts has_many :comments has_many :comments_received, :through =&gt; :posts, :source =&gt; :comments end class Post &lt; ActiveRecord::Base belongs_to :user has_many :comments end class Comment &lt; ActiveRecord::Base belongs_to :user belongs_to :post end </code></pre> <p>I'd like to be able to reference all the <code>comments_received</code> for a <code>user</code> with a route - let's say it's for batch approval of comments on all posts. <em>(note that you can also get <code>comments</code> made by the <code>user</code>, but users can't comment on their own posts, so the <code>comments</code> through a <code>post</code> are different and mutually exclusive)</em>. Logically, this should work with:</p> <pre><code>map.resources :users, :has_many =&gt; [:posts, :comments, :comments_received] </code></pre> <p>This should give me routes of</p> <pre><code>user_posts_path user_comments_path user_comments_received_path </code></pre> <p>The first two work, the last one doesn't. I've tried it without the <code>_</code> in <code>comments_received</code> to no avail. I'm looking to get a URL like</p> <pre><code>http://some_domain.com/users/123/comments_received </code></pre> <p>I've also tried nesting it, but maybe I'm doing that wrong. In that case, I'd think the map would be:</p> <pre><code>map.resources :users do |user| user.resources :comments user.resources :posts, :has_many =&gt; :comments end </code></pre> <p>and then the url might be:</p> <pre><code>http://some_domain.com/users/123/posts/comments </code></pre> <p>Maybe this is the right way to do it, but I have the syntax wrong?</p> <p>Am I thinking about this the wrong way? It seems reasonable to me that I should be able to get a page of all the <code>comments</code> added to all of a user's posts. </p> <p>Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload