Hi Everyone, I have issue while doing eager loading when url is => http://127.0.0.1:3001/projects/1/features I also did <%= debug @features %> to dump @feature object but I did not see ''owner'' being eager-loaded. Can anyone find out why owner is not being ''eager-loaded''? Thanks in advance. ========= features_controller.rb -> index method def index @project = Project.find(params[:project_id]) @features = @project.features.find(:all, :include => [:owner, :project]) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @features } end end ========= Output of <%= debug @features %> --- - !ruby/object:Feature attributes: project_id: "1" updated_at: 2009-10-24 11:07:19 created_at: 2009-10-24 11:07:19 attributes_cache: {} project: &id001 !ruby/object:Project attributes: status: In Progress title: TestProject id: "1" finished_on: 2009-12-31 23:59:00 owner_user_id: "4" brief: RAID Controller and DMA Engine for Invader created_at: 2009-10-20 19:35:15 attributes_cache: {} - !ruby/object:Feature attributes: project_id: "1" updated_at: 2009-10-24 11:26:09 details: for testing priority: normal id: "3" owner_id: "1" brief: test feature updated created_at: 2009-10-24 11:18:11 attributes_cache: {} project: *id001 ========= Error in browser NoMethodError in Features#index Showing app/views/features/index.html.erb where line #19 raised: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.username Extracted source (around line #19): 16: <td><%=h feature.details %></td> 17: <td><%=h feature.priority %></td> 18: <td><%=h feature.project.title %></td> 19: <td><%=h feature.owner.username %></td> 20: <td><%= link_to ''Show'', [@project, feature] %></td> 21: <td><%= link_to ''Edit'', edit_project_feature_path(@project, feature) %></td> 22: <td><%= link_to ''Destroy'', [@project, feature], :confirm => ''Are you sure?'', :method => :delete %></td> ========= Server Log ActionView::TemplateError (You have a nil object when you didn''t expect it! The error occurred while evaluating nil.username) on line #19 of app/ views/features/index.html.erb: 16: <td><%=h feature.details %></td> 17: <td><%=h feature.priority %></td> 18: <td><%=h feature.project.title %></td> 19: <td><%=h feature.owner.username %></td> 20: <td><%= link_to ''Show'', [@project, feature] %></td> 21: <td><%= link_to ''Edit'', edit_project_feature_path(@project, feature) %></td> 22: <td><%= link_to ''Destroy'', [@project, feature], :confirm => ''Are you sure?'', :method => :delete %></td> app/views/features/index.html.erb:19 app/views/features/index.html.erb:13:in `each'' app/views/features/index.html.erb:13 app/controllers/features_controller.rb:8:in `index'' -e:2:in `load'' -e:2 Rendered rescues/_trace (94.0ms) Rendered rescues/_request_and_response (0.0ms) Rendering rescues/layout (internal_server_error)
On Oct 24, 1:58 pm, nin_d <ninadpachp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Everyone, > > I have issue while doing eager loading when url is =>http://127.0.0.1:3001/projects/1/features > I also did <%= debug @features %> to dump @feature object but I did > not see ''owner'' being eager-loaded. > > Can anyone find out why owner is not being ''eager-loaded''? >Does the owner exist at all ? remove the :include, if the exception persists then you know eager loading is not at fault. Fred
Fred, owner do exist. class Feature < ActiveRecord::Base belongs_to :project belongs_to :owner, :class_name => "User", :foreign_key => "owner_id" end Now in features controller, I have @features = @project.features.find(:all, :include => :owner) and dump for @features in view is where i see only two features being loaded and not the corresponding owner (only 1 in this case, with id 1) - --- - !ruby/object:Feature attributes: project_id: "1" updated_at: 2009-10-24 11:07:19 details: Represent Single Source Mode opcode priority: nornal id: "2" owner_id: "1" brief: Opcode - SSM created_at: 2009-10-24 11:07:19 attributes_cache: {} - !ruby/object:Feature attributes: project_id: "1" updated_at: 2009-10-24 11:26:09 details: for testing priority: normal id: "3" owner_id: "1" brief: test feature updated created_at: 2009-10-24 11:18:11 attributes_cache: {}
Hi -- On Sat, 24 Oct 2009, nin_d wrote:> > Fred, owner do exist. > > class Feature < ActiveRecord::Base > belongs_to :project > belongs_to :owner, :class_name => "User", :foreign_key => "owner_id" > end > > Now in features controller, I have > > @features = @project.features.find(:all, :include => :owner) > > and dump for @features in view is where i see only two features being > loaded and not the corresponding owner (only 1 in this case, with id > 1) -Look again in your original post. There''s a debug dump of a Feature that has no owner_id. David -- The Ruby training with D. Black, G. Brown, J.McAnally Compleat Jan 22-23, 2010, Tampa, FL Rubyist http://www.thecompleatrubyist.com David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
Oops I had removed that myself. Damn! Consider this please - Now in features controller, I have @features = @project.features.find(:all, :include => :owner) and dump for @features in view is where i see only two features being loaded and not the corresponding owner (only 1 in this case, with id 1) - --- - !ruby/object:Feature attributes: project_id: "1" updated_at: 2009-10-24 11:07:19 details: some crap priority: nornal id: "2" owner_id: "1" brief: foo created_at: 2009-10-24 11:07:19 attributes_cache: {} - !ruby/object:Feature attributes: project_id: "1" updated_at: 2009-10-24 11:26:09 details: for testing priority: normal id: "3" owner_id: "1" brief: test feature updated created_at: 2009-10-24 11:18:11 attributes_cache: {}
In server log, I do see query for fetching users with id=1. How this is not appearing in the index view?? ========= Server Log Processing FeaturesController#index (for 127.0.0.1 at 2009-10-24 19:42:45) [GET] Parameters: {"project_id"=>"1"} [4;36;1mProject Columns (0.0ms) [0m [0;1mSHOW FIELDS FROM `projects` [0m [4;35;1mProject Load (0.0ms) [0m [0mSELECT * FROM `projects` WHERE (`projects`.`id` = 1) [0m [4;36;1mFeature Load (0.0ms) [0m [0;1mSELECT * FROM `features` WHERE (`features`.project_id = 1) [0m [4;35;1mFeature Columns (16.0ms) [0m [0mSHOW FIELDS FROM `features` [0m [4;36;1mUser Columns (16.0ms) [0m [0;1mSHOW FIELDS FROM `users` [0m [4;35;1mUser Load (0.0ms) [0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m Rendering template within layouts/features Rendering features/index [4;36;1mCACHE (0.0ms) [0m [0;1mSELECT * FROM `projects` WHERE (`projects`.`id` = 1) [0m [4;35;1mCACHE (0.0ms) [0m [0mSELECT * FROM `projects` WHERE (`projects`.`id` = 1) [0m Rendered common/_main_menu (16.0ms) Rendered common/_project_menu (0.0ms) Completed in 141ms (View: 47, DB: 32) | 200 OK [http://127.0.0.1/ projects/1/features]
On Oct 24, 3:14 pm, nin_d <ninadpachp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In server log, I do see query for fetching users with id=1. How this > is not appearing in the index view??Is there a user with id 1? Get it working without :include, then worry about the eager loading. Fred> > ========= Server Log > > Processing FeaturesController#index (for 127.0.0.1 at 2009-10-24 > 19:42:45) [GET] > Parameters: {"project_id"=>"1"} > [4;36;1mProject Columns (0.0ms) [0m [0;1mSHOW FIELDS FROM > `projects` [0m > [4;35;1mProject Load (0.0ms) [0m [0mSELECT * FROM `projects` > WHERE (`projects`.`id` = 1) [0m > [4;36;1mFeature Load (0.0ms) [0m [0;1mSELECT * FROM `features` > WHERE (`features`.project_id = 1) [0m > [4;35;1mFeature Columns (16.0ms) [0m [0mSHOW FIELDS FROM > `features` [0m > [4;36;1mUser Columns (16.0ms) [0m [0;1mSHOW FIELDS FROM `users` > [0m > [4;35;1mUser Load (0.0ms) [0m [0mSELECT * FROM `users` WHERE > (`users`.`id` = 1) [0m > Rendering template within layouts/features > Rendering features/index > [4;36;1mCACHE (0.0ms) [0m [0;1mSELECT * FROM `projects` WHERE > (`projects`.`id` = 1) [0m > [4;35;1mCACHE (0.0ms) [0m [0mSELECT * FROM `projects` WHERE > (`projects`.`id` = 1) [0m > Rendered common/_main_menu (16.0ms) > Rendered common/_project_menu (0.0ms) > Completed in 141ms (View: 47, DB: 32) | 200 OK [http://127.0.0.1/ > projects/1/features]
Fred, thanks! there s no user with id 1. My bad! While destroying a record, is there any easier way (a hook in activerecord) to get warning/error that record belongs to other records (if at all it does)?
On Oct 24, 4:26 pm, nin_d <ninadpachp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Fred, thanks! there s no user with id 1. My bad! > > While destroying a record, is there any easier way (a hook in > activerecord) to get warning/error that record belongs to other > records (if at all it does)?You can set a dependent option on associations to control this sort of stuff. If you want a real guarantee that this can''t ever happen, use a foreign key constraint. Fred