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
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.
–
“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!
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.
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!