Ruby-on-rails - Nested Resources Issue

I have a problem with nested resources.

2 Models

User => has_many :stuffs

Stuff => belongs_to :user

      

routes.rb

map.resources :stuffs

map.resources :users, :has_many => [:stuffs]

      

When I call / users / 1 / stuffs it presents me with stuff for the corresponding user. but I got this also when I call / users / 2 / stuff. It should return 0 "Stuffs", but it doesn't work.

MySQL Query from Server
SELECT * FROM `stuffs`

      

rake routes

stuffs GET    /stuffs(.:format)                         {:action=>"index", :controller=>"stuffs"}
                POST   /stuffs(.:format)                         {:action=>"create", :controller=>"stuffs"}
      new_stuff GET    /stuffs/new(.:format)                     {:action=>"new", :controller=>"stuffs"}
     edit_stuff GET    /stuffs/:id/edit(.:format)                {:action=>"edit", :controller=>"stuffs"}
          stuff GET    /stuffs/:id(.:format)                     {:action=>"show", :controller=>"stuffs"}
                PUT    /stuffs/:id(.:format)                     {:action=>"update", :controller=>"stuffs"}
                DELETE /stuffs/:id(.:format)                     {:action=>"destroy", :controller=>"stuffs"}
    user_stuffs GET    /users/:user_id/stuffs(.:format)          {:action=>"index", :controller=>"stuffs"}
                POST   /users/:user_id/stuffs(.:format)          {:action=>"create", :controller=>"stuffs"}
 new_user_stuff GET    /users/:user_id/stuffs/new(.:format)      {:action=>"new", :controller=>"stuffs"}
edit_user_stuff GET    /users/:user_id/stuffs/:id/edit(.:format) {:action=>"edit", :controller=>"stuffs"}
     user_stuff GET    /users/:user_id/stuffs/:id(.:format)      {:action=>"show", :controller=>"stuffs"}
                PUT    /users/:user_id/stuffs/:id(.:format)      {:action=>"update", :controller=>"stuffs"}
                DELETE /users/:user_id/stuffs/:id(.:format)      {:action=>"destroy", :controller=>"stuffs"}
          users GET    /users(.:format)                          {:action=>"index", :controller=>"users"}
                POST   /users(.:format)                          {:action=>"create", :controller=>"users"}
       new_user GET    /users/new(.:format)                      {:action=>"new", :controller=>"users"}
      edit_user GET    /users/:id/edit(.:format)                 {:action=>"edit", :controller=>"users"}
           user GET    /users/:id(.:format)                      {:action=>"show", :controller=>"users"}
                PUT    /users/:id(.:format)                      {:action=>"update", :controller=>"users"}
                DELETE /users/:id(.:format)                      {:action=>"destroy", :controller=>"users"}
           root        /                                         {:action=>"index", :controller=>"users"}

      

gem list

actionmailer (2.3.8)
actionpack (2.3.8)
activerecord (2.3.8)
activeresource (2.3.8)
activesupport (2.3.8)
arel (2.0.6)
authlogic (2.1.6)
builder (2.1.2)
cgi_multipart_eof_fix (2.5.0)
gem_plugin (0.2.3)
i18n (0.5.0)
mongrel (1.1.5 x86-mingw32)
mysql (2.8.1 x86-mingw32)
paperclip (2.3.7)
rack (1.1.0)
rails (2.3.8)
rake (0.8.7)
tzinfo (0.3.23)

      

There is no where clause for the corresponding user_id. But how do you fix it?

Rails Version 2.3.8

Should work like this => http://guides.rubyonrails.org/routing.html 2.7 Included Resources

Hope someone can help

+1


source to share


4 answers


In your Stuffs controller index method, how do you collect your stuff? If you used the scaffold to create a controller, it will by default have something like

@stuffs = Stuff.all

      

but should be something like lines



@user = User.find(params[:user_id])
@stuffs = @user.stuffs

      

or something like that - you basically collect user material, not everything.

+5


source


Rails controllers will not filter your results by default. As above, since you are probably calling Stuff.all in your StuffsController, it will always return all Stuff objects.

I am using inherited_resources for the default behavior on my sites. It handles these relationships automatically and allows you to override it when you need different behavior:



https://github.com/josevalim/inherited_resources

+3


source


Routes alone do not affect the model. Since the user ID is being passed, you can:

User.find(params[:user_id]).stuffs

      

0


source


try it

map.resources :users do |users|
  users.resources :stuffs
end

      

0


source







All Articles