nin_d
(nin_d)
October 24, 2009, 12:58pm
1
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 %>
Does the owner exist at all ? remove the :include, if the exception
persists then you know eager loading is not at fault.
Fred
nin_d
(nin_d)
October 24, 2009, 1:46pm
3
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) -
nin_d
(nin_d)
October 24, 2009, 2:02pm
5
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) -
nin_d
(nin_d)
October 24, 2009, 2:14pm
6
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"}
e[4;36;1mProject Columns (0.0ms)e[0m e[0;1mSHOW FIELDS FROM
`projects`e[0m
e[4;35;1mProject Load (0.0ms)e[0m e[0mSELECT * FROM `projects`
WHERE (`projects`.`id` = 1) e[0m
e[4;36;1mFeature Load (0.0ms)e[0m e[0;1mSELECT * FROM `features`
WHERE (`features`.project_id = 1) e[0m
e[4;35;1mFeature Columns (16.0ms)e[0m e[0mSHOW FIELDS FROM
`features`e[0m
e[4;36;1mUser Columns (16.0ms)e[0m e[0;1mSHOW FIELDS FROM `users`e
[0m
e[4;35;1mUser Load (0.0ms)e[0m e[0mSELECT * FROM `users` WHERE
(`users`.`id` = 1) e[0m
Rendering template within layouts/features
Rendering features/index
e[4;36;1mCACHE (0.0ms)e[0m e[0;1mSELECT * FROM `projects` WHERE
(`projects`.`id` = 1) e[0m
e[4;35;1mCACHE (0.0ms)e[0m e[0mSELECT * FROM `projects` WHERE
(`projects`.`id` = 1) e[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]
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
nin_d
(nin_d)
October 24, 2009, 3:26pm
8
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