Rails map.resources with has_many: through not working?

I have three (relevant) models specified as follows:

class User < ActiveRecord::Base
  has_many :posts
  has_many :comments
  has_many :comments_received, :through => :posts, :source => :comments
end

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :post
end

I would like to be able to link everything comments_receivedto userusing the route - let him talk about approving comment comments on all posts. (note that you can also get commentsdone user, but users cannot comment on their own posts, so commentsthrough a postthey are different from each other and mutually excluded). Logically this should work with:

map.resources :users, :has_many => [:posts, :comments, :comments_received]

That should give me routes

user_posts_path
user_comments_path
user_comments_received_path

The first two works, and the last not. I tried it without _to comments_receivedno avail. I am looking to get a url like

http://some_domain.com/users/123/comments_received

, , , . , :

map.resources :users do |user|
  user.resources :comments
  user.resources :posts, :has_many => :comments
end

URL :

http://some_domain.com/users/123/posts/comments

, , ?

? , comments, .

!

+3
3

deinfe , , , . , .

, , , . , :

map.resources :users do |user|
  user.resources :posts
  user.resources :comments
  user.resources :comments_received
end

'rake routes', ( !):

                       users GET /users                              {:action=>"index", :controller=>"users"}
                  user_posts GET /users/:user_id/posts               {:action=>"index", :controller=>"posts"}
               user_comments GET /users/:user_id/comments            {:action=>"index", :controller=>"comments"}
user_comments_received_index GET /users/:user_id/comments_received   {:action=>"index", :controller=>"comments_received"}

, , _index comments_received. , , (- ?), .

, :

map.resources :users do |user|
  user.resources :posts
  user.resources :comments, :collection => {:received => :get}
end

:

                 users GET /users                             {:action=>"index", :controller=>"users"}
            user_posts GET /users/:user_id/posts              {:action=>"index", :controller=>"posts"}
         user_comments GET /users/:user_id/comments           {:action=>"index", :controller=>"comments"} 
received_user_comments GET /users/:user_id/comments/received  {:action=>"received", :controller=>"comments"}

:

+2

, , , , has_many: . , , ?

-, , , foos . - :

map.resources :users do |user|
  user.resources :awards
  user.resources :contest_entries do |contest_entry|
    contest_entry.resources :awards
  end
end

, , :

user_path, user_awards_path, user_contest_entry_path, and user_contest_entry_awards_path.

, , , , foo, bar baz - .

0

A quick n-dirty solution would be to add a user method (e.g. getusercomments) to your user controller, which will return all comments:

def getusercomments
@user = User.find(params[:id])
@comments = @user.posts.comments
end

Then add this method to your users route:

map.resources :users, :member => { :getusercomments => :get }

After that, you can call the following to get all user comments:

http://some_domain.com/users/123/getusercomments
0
source

Source: https://habr.com/ru/post/1699428/


All Articles