Forum: Ruby on Rails Disabling ActiveRecord "type" column subclassing

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.
2fa87d4eaa7d6a499cf05527f8898428?d=identicon&s=25 Allen Fair (allen)
on 2007-03-28 17:34
Hi! I have a legacy table that has a column named "type" and need to
remove this automatic subclassing by ActiveRecord. The api pages are
vague about this, and I tried

self.inheritance_column = "xtype"

which seemed to get me half-way. In the validate_on_create method, the
value of self.type was my model Class name.

How would you disable this feature?
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-03-28 17:44
(Received via mailing list)
> Hi! I have a legacy table that has a column named "type" and need to
> remove this automatic subclassing by ActiveRecord. The api pages are
> vague about this, and I tried
>
> self.inheritance_column = "xtype"
>
> which seemed to get me half-way. In the validate_on_create method, the
> value of self.type was my model Class name.
>
> How would you disable this feature?

If you want to check 'type' do self['type']..
588ab1c0a5610a7e160a3b101abb91e6?d=identicon&s=25 MichaelLatta (Guest)
on 2007-03-29 04:26
(Received via mailing list)
The short answer is that you can not disable this.  The 'type' method
is a Ruby method for the class of an object.  As stated by Philip you
can access the 'type' column data by directly indexing the active
record.  You can also create a facade by having methods on the class
that retrieve the 'type' column using a different name as in:

def legacy_type
  return self[:type]
end

def legacy_type=(value)
  self[:type] = value
end

Michael



On Mar 28, 8:34 am, Allen Fair <rails-mailing-l...@andreas-s.net>
A4dec2fd09973a90b713806ad07b26c4?d=identicon&s=25 Jaime B. (jaime_b)
on 2013-02-19 01:01
MichaelLatta wrote in post #227241:
> The 'type' method is a Ruby method for the class of an object.

This is not a Ruby method.  This is part of ActiveRecord, and is used by
Rails to manage single-table inheritance.

I'm adding a solution for future askers. This applies to Rails 3:

class ExampleModel < ActiveRecord::Base
  set_inheritance_column do
    'disabled'
  end
end


This changes the name of the column that ActiveRecord looks for, to use
for STI.  As long as you pick something that doesn't actually match any
of your column names, it has the effect of disabling inheritance
altogether.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2013-02-19 14:49
(Received via mailing list)
On Feb 18, 2013, at 7:01 PM, Jaime B. wrote:

> MichaelLatta wrote in post #227241:
>> The 'type' method is a Ruby method for the class of an object.
>
> This is not a Ruby method.  This is part of ActiveRecord, and is used by
> Rails to manage single-table inheritance.

While it is true that 'type' is not a method in Ruby 1.9, it was in
earlier versions:

irb1.8.7> 1.type
(irb):1: warning: Object#type is deprecated; use Object#class
#1.8.7 => Fixnum

It is something to keep in mind whenever looking at older code.

-Rob
321a3064533f836c31605bd63077bb50?d=identicon&s=25 Utile Bout (utile_b)
on 2014-02-26 21:40
Simply use
class MyModel < ActiveRecord::Base
  self.inheritance_column = 'somthing_not_type'
end
This topic is locked and can not be replied to.