Forum: Ruby on Rails Single table inheritance - HowTo?

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.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-01-06 23:01
(Received via mailing list)
Hi,

As per another member's suggestion, I'm trying to user STI. So I have
this table:

CREATE TABLE `settings` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `type` char(40) NOT NULL default 'General',
  `name` char(40) NOT NULL default '',
  `value` char(128) NOT NULL default '',
  `updated_on` datetime default NULL,
  `comments` char(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

And a row with a type = 'General', and name = 'front-page'

Then I have a setting.rb model, and also general_setting.rb and
user_setting.rb, where I only have:

class <TYPE>Setting < Setting
end

So, if I do General.find_by_name('front-page'), I expected to get the
value of the corresponding row, instead I get a nil object.

What could I possibly be doing wrong?

Thanks!

Ivan V.
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 Matthew Palmer (Guest)
on 2006-01-07 01:01
(Received via mailing list)
[Note: HowTo in the subject suggests you're providing a HowTo.  You're
not.
You're asking a question.]

On Fri, Jan 06, 2006 at 03:59:18PM -0600, Iván Vega Rivera wrote:
>  PRIMARY KEY  (`id`)
> So, if I do General.find_by_name('front-page'), I expected to get the
> value of the corresponding row, instead I get a nil object.

The value of the type field should be the name of the subclass you wish
to
instantiate for that row.  So if your class is named GeneralSetting, you
should be inserting type='GeneralSetting'.

- Matt
0091f92762685860109bbcb02edfdf27?d=identicon&s=25 Alain Ravet (Guest)
on 2006-01-07 11:08
(Received via mailing list)
Iván

The 'type' column is used by Rails in the background and, most of the
time, there is no need for you to manipulate it explicitly.


So, if you have
    class Foo < ActiveRecord
and
    class SuperFoo < Foo

, you can query them directly:

    allRecords = Foo.find(:all)
    onlyTheSuperFoos = SuperFoo.find(:all)

Did that answer your question?

Alain
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-01-07 18:42
(Received via mailing list)
Matthew, although a bit crankyly ;-), had the answer. I wasn't using the
correct type name for each class.

Although you do make a point. I should've used your method to test how
STI worked in the first place.

Thanks!

Ivan V.
This topic is locked and can not be replied to.