Forum: Ruby on Rails how to map one-to-one relation to two tables

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.
Rest L. (Guest)
on 2006-04-07 17:53
hi. iv got a problem with one-to-one relation with AR. let's say I need
this classes:

class ContentObject < ActiveRecord::Base
class BlogEntry < ContentObject
class FotoEntry < ContentObject

i.e. Blog and Foto entries must extend ContentObject. Also they must be
mapped to separate tables. I can not figure out how to map this kind of
association better?

The most dumb and straitforward way is actually NOT to extend
ContentObject and associate it as regular has-one and belongs-to, but
that way all beauty will b gone, since i'll need manually treat object
instantiation etc., maybe there are much more elegant ways to solve this
kind of problems?
Alex Y. (Guest)
on 2006-04-07 18:27
(Received via mailing list)
rest less wrote:
> hi. iv got a problem with one-to-one relation with AR. let's say I need
> this classes:
>
> class ContentObject < ActiveRecord::Base
> class BlogEntry < ContentObject
> class FotoEntry < ContentObject
> i.e. Blog and Foto entries must extend ContentObject. Also they must be
> mapped to separate tables. I can not figure out how to map this kind of
> association better?
>

How about:

module ContentBehaviour
   #whatever behaviour is shared
end

class BlogEntry < ActiveRecord::Base
   include ContentBehaviour
end

class FotoEntry < ActiveRecord::Base
   include ContentBehaviour
end
This topic is locked and can not be replied to.