help active record modeling.

Hi.

I have difficulties to model active record.
So please help my trouble.

Let's suppose there are nations which have castles at least 1,
and there are 3 types of castles.

So I will make these tables.
NationsTable,
CastlesTable have common attributes of 3 types of castle.
    CastlesATable, CastlesBTable, CastlesCTable each have its unique
attributes.

In this case how can I represent this DB to AR?
I think
class Nation < ActiveRecord::Base
   has_many :castles
end
class Castle < ActiveRecord::Base
  belongs_to :Nations
end

but I'm not sure of what will be coded to these models.

class CastleA < ActiveRecord::Base
end
class CastleB < ActiveRecord::Base
end
class CastleB < ActiveRecord::Base
end

please help me.

and second question is
when nation deleted, CastleA, CastleB have to be deleted but not for
the castleC type.
how can I do?

thanks for reading.

Hi.

I have difficulties to model active record.

So please help my trouble.

Let’s suppose there are nations which have castles at least 1,

and there are 3 types of castles.

So I will make these tables.

NationsTable,

CastlesTable have common attributes of 3 types of castle.

CastlesATable, CastlesBTable, CastlesCTable each have its unique

attributes.

Easier to call your tables: ‘nations’ for ‘Nation’ model and ‘castles’ for ‘Castle’ model … etc

In this case how can I represent this DB to AR?

I think

class Nation < ActiveRecord::Base

has_many :castles

end

class Castle < ActiveRecord::Base

belongs_to :Nations

end

Using the naming convention I suggested above you’d say:
belongs_to :nation

After all, a castle can only belong to one nation at a time right?

but I’m not sure of what will be coded to these models.

class CastleA < ActiveRecord::Base

end

class CastleB < ActiveRecord::Base

end

class CastleB < ActiveRecord::Base

end

please help me.

You could use single table inheritance (STI).

  1. all castles are stored in one table (‘castles’) which has all the required attributes for all types of castles.

  2. in app/models you’ll have your Castle model and then you’ll have a CastleA,B and C models like above but you now inherit from Castle:

    class CastleA < Castle
    end

  3. You include a ‘type’ field in ‘castles’ table which is a string and
    is the same as the class name of the castle. (ie for an A-type it would be ‘CastleA’)

This is a tidy way to handle your castles as everything is in one
table. It does mean that not all attributes will be used by a given
castle instance if you have specialised attributes; these will probably be null so I don’t think the
wastage will be much.

and second question is

when nation deleted, CastleA, CastleB have to be deleted but not for

the castleC type.

how can I do?

I’m never good at this stuff. But you could do it with a callback in your Nation model:

class Nation < ActiveRecord::Base
has_many :castles
before_destroy do |nation|
Castle.destroy_all “nation_id=#{nation.id} and type!=’#{CastleC}’”

end
end

There’s probably a sexier way to do that.