Newbie Model question (HABTM?)

All,

If I have a table that has several discrete fields that all point to
the same associated field, how do I model it.

Specifically,

If I have a project table that has 2 fields: specifying_company, and
responsible_company.

And one table of associated companies, how do I do the :belongs_to /
:has_many stuff

ie. For a given project company A may be writing the spec, but company
B may be paying the bill. For another project it is possible the
arrangement is reverse.

I’m guessing this is a HABTM situation, but how do I do the
:foreign_key and :class_name setup.

Greg

Greg F.
The Norcross Group
Forensics for the 21st Century

Hi –

On Thu, 6 Jul 2006, Greg F. wrote:

And one table of associated companies, how do I do the :belongs_to /
:has_many stuff

ie. For a given project company A may be writing the spec, but company
B may be paying the bill. For another project it is possible the
arrangement is reverse.

I’m guessing this is a HABTM situation, but how do I do the
:foreign_key and :class_name setup.

Try this:

In project.rb:

class Project < ActiveRecord::Base
belongs_to :specifying_company,
:class_name => “Company”,
:foreign_key => “specifying_company_id”
belongs_to :responsible_company,
:class_name => “Company”,
:foreign_key => “responsible_company_id”
end

In company.rb:

class Company < ActiveRecord::Base
has_many :projects_specified_by,
:class_name => “Project”,
:foreign_key => “specifying_company_id”
has_many :projects_responsible_for,
:class_name => “Project”,
:foreign_key => “responsible_company_id”
end

In the projects database table, you would have the two necessary
foreign keys. You can then do things like:

pr = Project.create
pr.specifying_company = some_company

some_company.projects_specified_by # includes pr, possibly others

etc.

David


“To fully realize the potential of Rails, it’s crucial that you take
the time to fully understand Ruby–and with “Ruby for Rails” David
has provided just what you need to help you achieve that goal.”
– DAVID HEINEMEIER HANSSON, in the foreword to RUBY FOR RAILS.
Complete foreword & sample chapters at Ruby for Rails!

Greg F. wrote:

If I have a project table that has 2 fields: specifying_company, and
responsible_company.

And one table of associated companies, how do I do the :belongs_to /
:has_many stuff

Assuming I understood your tables and models correctly, it would look
something like this…

(in the “project” table)
belongs_to :specifying_company, :class_name => “Project”, :foreign_key
=> “specifying_company_id”
belongs_to :responsible_company, :class_name => “Project”,
:foreign_key => “responsible_company_id”

The first thing just says what you want to call the relationship on your
model. Then the foreign_key says what underlying field/property it is
linked to.

-Mark E.

Solved,

I really appreciate it. I’m not sure what I was doing different, but
it was not working with my efforts.

Greg

On 7/6/06, [email protected] [email protected] wrote:

I’m guessing this is a HABTM situation, but how do I do the
belongs_to :responsible_company,
has_many :projects_responsible_for,
some_company.projects_specified_by # includes pr, possibly others
– DAVID HEINEMEIER HANSSON, in the foreword to RUBY FOR RAILS.
Complete foreword & sample chapters at Ruby for Rails!


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Greg F.
The Norcross Group
Forensics for the 21st Century