Newbie: Going round in circles with HABTM

Hi,
I’m still stuck with a fundamental problem understanding how this hangs
together. Given two tables with a HABTM relationship through a
join_table as follows;

Model job:
class Job < ActiveRecord::Base
belongs_to :JobTypes
has_many :People
has_and_belongs_to_many :addresses, :join_table => “Jobs_Addresses”
has_many :Reminders
has_many :JobPhases
has_many :Discussions
has_many :actions
has_one :jobtype
has_many :documents
has_one :organisation
has_many :reminders
has_many :discussions
validates_associated :address
end

Model address:
class Address < ActiveRecord::Base
has_and_belongs_to_many :jobs, :join_table => “Jobs_Addresses”
has_one :Address_type
has_one :organisation
end

Model job_address
class JobAddress < ActiveRecord::Base
has_and_belongs_to_many :jobs
has_and_belongs_to_many :addresses
end

OK, heres my problem =>
In console I get to a job.
I can read its data, methods and attributes and I can return the address
data as follows;

@job.addresses
=> [#<Address:0x37dd170 @attributes={“Organisation_ID”=>nil,
“Address_2”=>nil, “Job_ID”=>“4”, “Country”=>“Australia”, “id”=>“2”,
“Suburb”=>“Mitcham”, “OrgType_ID”=>nil, “State”=>“Vic”,
“Address_1”=>“456, second st”, “Address_ID”=>“2”}>]

What I can’t figure is how to get at that attribute data to put into a
simple job list mixing the address in with the other job attributes!

Can somebody please explain before I go nuts!!

Kind Regards,
Eric.

On 12/2/05, Eric S. [email protected] wrote:

has_and_belongs_to_many :addresses, :join_table => “Jobs_Addresses”

If you rename your join table alphabetically (ie addresses_jobs) then
you
won’t have to specify :join_table because the Rails convention will find
it
automatically.

@job.addresses
=> [#<Address:0x37dd170 @attributes={“Organisation_ID”=>nil,
“Address_2”=>nil, “Job_ID”=>“4”, “Country”=>“Australia”, “id”=>“2”,
“Suburb”=>“Mitcham”, “OrgType_ID”=>nil, “State”=>“Vic”,
“Address_1”=>“456, second st”, “Address_ID”=>“2”}>]

What I can’t figure is how to get at that attribute data to put into a
simple job list mixing the address in with the other job attributes!

Since a job may have more than one address, maybe you want to do this

<% @job.addresses.each do |address| %>
<%= address.Suburb %>, <%= address.Country %>
<% end %>

Also, I think that Rails people do not usually capitalize db table and
field
names. If you are using a legacy db then this comment and the one about
the
join table name are probably just annoying:)

Hope that helps.

Peter

Peter M. wrote:

you won’t have to specify :join_table because the Rails convention will

Also, I think that Rails people do not usually capitalize db table and


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks Peter,
I knew it was something simple! The only annoying thing is the headache
I have from trying to figure this out!!
Thanks Again,
Eric.