Has and belongs to many different?

Is there such a thing? I am thinking of an application that has
something like this:

  1. person has many things - there is a ‘persons’ table

  2. the things he has are stored in a table that is something like this
    person_id, thing_table_name, thing_id
    For example, it may map to
    person1, books, 23 #Person 1 owns book 23
    person1, books, 25 #Person 1 owns book 25
    person1, chair, 23 #Person 1 owns chair 23
    person1, books, 30
    person2, books, 19
    person2, books, 31 #Person 2 owns book 31
    … and so on…

  3. A number of tables for different ‘things’…
    books - table for books
    chairs - table for chairs
    and so on…

Ideally I want to have something which means “person
has_and_belongs_to_many_different things”
I guess it could be implemented by manually looking at the 2nd table to
find the foreign key and table that it refers to and then do a find from
the specific table for the specific ID. But, I’m trying to find a
better “Railsy” way.

Cheers
Mohit.

Mohit S. wrote:

person1, chair, 23 #Person 1 owns chair 23
Ideally I want to have something which means “person
has_and_belongs_to_many_different things”
I guess it could be implemented by manually looking at the 2nd table
to find the foreign key and table that it refers to and then do a find
from the specific table for the specific ID. But, I’m trying to find a
better “Railsy” way.

Cheers
Mohit.
Answering my own question here… but is this similar to Recipe #23 in
the Rails Recipes book? Is this a case of “Polymorphic
Associations?has_many :whatevers”?

Cheers
Mohit.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs