Multiple table relationships


#1

Hello,

I’m just starting out with some Rails development and have the following
table structure. I understand how to do a two table has many join but I
actually need to relate a third table. Here is my structure:

components
id
name
updated_at

elements
id
name
updated_at

element_types
id
name
updated_at

components_elements
component_id
element_id
element_type_id
updated_at

My Component model has the following:

class Component < ActiveRecord::Base
has_and_belongs_to_many :elements
end

I haven’t actually added anything to my Element model but suspect that
it
also needs a habtm association. My bigger issue is, how to I associate
things with the element_types table?

Kyle H.
removed_email_address@domain.invalid
www.kyleheon.com


#2

On 24/03/06, Kyle H. removed_email_address@domain.invalid wrote:

name
updated_at
has_and_belongs_to_many :elements


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Well I haven’t been doing this long, but hopefully I won’t make any
mistakes here.

class Component < ActiveRecord::Base
has_and_belongs_to_many :elements
end

class Element < ActiveRecord::Base
has_and_belongs_to_many :components
belongs_to :element_type
end

class ElementType < ActiveRecord::Base
has_many :elements
end

Hopefully that’s answered your question.
-Nathan