Hi Peter,
> Project.rb
>
> class Project < ActiveRecord::Base
>
> has_one :project_manager, :class_name => "Users",
:foreign_key =>
> "manager_id"
>
> end
I think you are talking about a one-to-many relationship here. One
project has one project manager but one user can be a project manager
in many projects (old and new).
In that case, you need to add ''has_many :project_in_leads, :class_name
=> "Project", :foreign_key => "manager_id" ''
to your User class.
Actually, that isn''t necessary if you never want to approach projects
from the user perspective ("Which projects has this guy lead?").
But anyway, you need to use belongs_to instead of has_one in your
Project class. If you use has_one, it means that the foreign key field
is on the other side of the relationship (i.e. in users table), which
isn''t the case here.
So consider changing the line to ''belongs_to :project_manager,
:class_name => "User", :foreign_key => "manager_id"
'' .
> But I get an error:-
>
> Showing /projects/list.rhtml where line #35 raised
> c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/
> support/dependencies.rb:59:in `const_missing'': uninitialized
constant
> Users
This is because you have ":class_name => "Users"" in
Project class
(should be "User"). But you also need to do the changes above to make
it work right.
> Also if the above concept can be made to work then I suppose it could
> be quite heavy since it must be doing a single select for each project
> to get the manager info. Or is this done relationally in the
> controller based on the Model definitions.
I think it will be repeated for every row. You can inspect the queries
by looking at the development.log (assuming you''re in dev state) in
your application''s log folder.
//jarkko
--
Jarkko Laine
http://jlaine.net
http://odesign.fi
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails