Forum: Ruby on Rails Multiple table relationships

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.
Kyle H. (Guest)
on 2006-03-24 02:27
(Received via mailing list)
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
unknown (Guest)
on 2006-03-24 02:37
(Received via mailing list)
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
This topic is locked and can not be replied to.