ruby on rails - Problem with associations -
i have built ruby on rails app lets users track workouts. user has_many workouts. in addition, user can create box (gym) if gym owner. purpose filter activity of users such can see information related gym. in case workouts. on box show page...i show users associated box through membership , consequently associated users workouts. here set up.
user
class user < activerecord::base has_many :boxes has_many :workouts, :dependent => :destroy end
workout
class workout < activerecord::base belongs_to :user belongs_to :box end
box
class box < activerecord::base belongs_to :user has_many :users, :through => :memberships has_many :workouts, :through => :users has_many :memberships end
membership
class membership < activerecord::base belongs_to :user belongs_to :box end
in /views/boxes/show.html.erb view have following:
<% @box.workouts.each |workout| %> <%= workout.title %><br/> <%= workout.user.username %><br/><br/> <% end %>
which produces no error...but no results. here output log
processing boxescontroller#show (for 127.0.0.1 @ 2010-12-01 22:19:59) [get] parameters: {"id"=>"7"} user load (0.4ms) select * "users" ("users"."id" = '1') limit 1 box load (0.3ms) select * "boxes" ("boxes"."id" = 7) cache (0.0ms) select * "boxes" ("boxes"."id" = 7) user load (0.4ms) select * "users" ("users"."id" = 1) workout load (0.3ms) select "workouts".* "workouts" inner join "users" on "users".id = "workouts".user_id inner join "boxes" on "boxes".id = "workouts".box_id ("workouts"."user_id" = 1) , ((("workouts"."public" = 1) , (("users".box_id = 7))) , (("users".box_id = 7))) order created_at desc rendering template within layouts/application rendering boxes/show user load (0.6ms) select "users".* "users" inner join "memberships" on "users".id = "memberships".user_id (("memberships".box_id = 7)) workout load (0.2ms) select "workouts".* "workouts" inner join "users" on "workouts".user_id = "users".id (("users".box_id = 7)) rendered shared/_navigation (0.6ms) completed in 104ms (view: 23, db: 2) | 200 ok [http://localhost/boxes/7]
thoughts on why isn't working?
i think problem associations users memberships box gets mixed might have tried earlier. because if @ last time workout loaded in log:
workout load (0.2ms) select "workouts".* "workouts" inner join "users" on "workouts".user_id = "users".id (("users".box_id = 7))
the condition indicates user belongs_to box can't see code. , since don't errors line, field has exist in database , should remove it.
further, have specified user has_many boxes, seems have forgotten add through :memberships. guess correct way be:
class user < activerecord::base has_many :memberships has_many :boxes, :through => :memberships has_many :workouts, :dependent => :destroy end
and finally, combining 2 through associations not work afaik, @ least not in setup. if want workouts users in specific box, this:
#in controller @box_users = @box.users.all(:include => :workouts) #in view <% @box_users.each |user| %> <% user.workouts.each |workout| %> <%= workout.title %> <%= user.username %> <% end %> <% end %>
the call :include of course not necessary generate 1 sql query instead of 1+n
Comments
Post a Comment