Forum: Ruby on Rails help active record modeling.

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.
67259181b4d628f350f5edfafcea6520?d=identicon&s=25 serenobs (Guest)
on 2009-04-28 21:49
(Received via mailing list)
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.
9b4c04c050122bcea16a6f3376d680fa?d=identicon&s=25 Daniel Bush (danb)
on 2009-04-29 02:32
(Received via mailing list)
2009/4/29 serenobs <serenobs@gmail.com>

> 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?


>
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.




--
Daniel Bush

http://blog.web17.com.au
http://github.com/danielbush/sifs/tree/master
http://github.com/danielbush
This topic is locked and can not be replied to.