Forum: Ruby on Rails newbie: Going round in circles with HABTM

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
esloane (Guest)
on 2005-12-03 07:22
(Received via mailing list)
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.
petermichaux (Guest)
on 2005-12-03 07:59
(Received via mailing list)
On 12/2/05, Eric S. <removed_email_address@domain.invalid> 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
esloane (Guest)
on 2005-12-03 08:27
(Received via mailing list)
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
> removed_email_address@domain.invalid
> 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.
This topic is locked and can not be replied to.