rails map.resources with has_many :through doesn't work?

后端 未结 3 1509
北恋
北恋 2021-01-03 09:27

I\'ve got three (relevant) models, specified like this:

class User < ActiveRecord::Base
  has_many :posts
  has_many :comments
  has_many :comments_receiv         


        
3条回答
  •  一生所求
    2021-01-03 09:59

    A quick-n-dirty solution would be to add a custom method (e.g. getusercomments) to your users-controller that would return all the 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 }
    

    Afterwards you should be able to call the following to get all comments of a user:

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

提交回复
热议问题