Topic: Table could "belong_to" one of many

I'm going talk about this in a somewhat abstract way... Note: I'm not working with gardens in my database tongue.

We have many companies.

We have many buildings.
A company can have many buildings.

We have many plants.
A building can have many plants (which implies a company has many plants).
A company can have many plants (which are not associated with a building).

We have many pebbles.
A plant can have many pebbles (which implies a building and a company have many pebbles).
A building can have many pebbles (which are not associated with a plant, but are associated with a company).
A company can have many pebbles (which are not associated with a building or a plant).

In my database I've worked this out using 'owner' and 'owner_id' columns. However, how do I represent this in my model using :has_many and :belongs_to? Each plant doesn't necessarily belong_to a building, and the standard mapping of 'company_id' doesn't exist.

Re: Table could "belong_to" one of many

KittyKate wrote:

In my database I've worked this out using 'owner' and 'owner_id' columns.

I don't quite follow. What do you mean by using "owner" and "owner_id" columns? In which table?

KittyKate wrote:

Each plant doesn't necessarily belong_to a building

Although the language "belongs_to" doesn't really fit, it's still the correct choice. Don't worry about if it truely belongs_to the other model, it is just Rails' way of representing a one-to-many association.

Railscasts - Free Ruby on Rails Screencasts

Re: Table could "belong_to" one of many

Thanks Ryan!

For clarification, I've got 'owner' and 'owner_id' on the 'plant' and 'pebble' tables.

Re: Table could "belong_to" one of many

Could you expound on that some more? Which table do they link to? What does the "owner" column do that isn't accomplished by "owner_id"?

Railscasts - Free Ruby on Rails Screencasts

Re: Table could "belong_to" one of many

Owner identifies which table you are relating to, owner_id is the primary key (id) of that table.

So:

CompanyA:
  id:1
  name:Company A

BuildingA:
  id:1
  company_id:1
BuildingB:
  id:2
  company_id:1

PlantA:
  id:1
  owner:building
  owner_id:1
PlantB:
  id:2
  owner:company
  owner_id:1

PebbleA:
  id:1
  owner:plant
  owner_id:2

Re: Table could "belong_to" one of many

I recommend looking into polymorphic association. It is a better way to do this with Rails. Just call it "owner_type" instead of "owner" and you can reference different models/tables that way.

class Plant < ActiveRecord::Base
  belongs_to :owner, :polymorphic => true
end

class Pebble < ActiveRecord::Base
  belongs_to :owner, :polymorphic => true
end

class Company < ActiveRecord::Base
  has_many :plants, :as => :owner
  has_many :pebbles, :as => :owner
end

class Building < ActiveRecord::Base
  has_many :pebbles, :as => :owner
  has_many :plants, :as => :owner
end


Of course you would add the other one-to-many associations too.

Railscasts - Free Ruby on Rails Screencasts

Re: Table could "belong_to" one of many

Cool. smile.