hey all,
I have a table like that:
devices(
id int(5)
uid int(5)
type varchar(5)
)
this is my model:
class Rdevice < ActiveRecord::Base
set_table_name “devices”
end
for legacy reason I cannot change the column names. So I get this
error when I do
Rdevice.find :all
“ActiveRecord::SubclassNotFound: The single-table inheritance
mechanism failed to locate the subclass: ‘uuid’. This error is raised
because the column ‘type’ is reserved for storing the class in case of
inheritance. Please rename this column if you didn’t intend it to be
used for storing the inheritance class or overwrite
Rdevice.inheritance_column to use another column for that
information.”
Any idea how I can fix it? I know there is a inheritance_column
function to overwrite it but I couldn’t get it to work.
thanx in advance
Pat
Hi Patrick,
I’ve had a similar problem when trying to create an AR model for our
legacy database. You can’t use “type” as a column name because active
record uses it to store the ruby class name (for single table
inheritance). Furthermore, “type” is a protected word in Ruby which
again, stores the class name - so even if you change the inheritance
column you still have to access the variable from some other name other
than self.type
ideally you just don’t use “type” as a column name, but if you
absolutely need to you can use set_inheritance_column() method to rename
it to something else (I always rename mine to “ruby_type” regardless of
the application). then you can write some simple methods to handle
accessing the attribute without using that pesky reserved word.
so for your specific example…
hey all,
I have a table like that:
devices(
id int(5)
uid int(5)
type varchar(5)
)
Your model should look like this
class Rdevice < ActiveRecord::Base
set_table_name “devices”
set_inheritance_column :ruby_type
getter for the “type” column
def device_type
self[:type]
end
setter for the “type” column
def device_type=(s)
self[:type] = s
end
end
Enjoy!
-Brian
Brian C. wrote:
Hi Patrick,
so for your specific example…
hey all,
I have a table like that:
devices(
id int(5)
uid int(5)
type varchar(5)
)
Your model should look like this
class Rdevice < ActiveRecord::Base
set_table_name “devices”
set_inheritance_column :ruby_type
getter for the “type” column
def device_type
self[:type]
end
setter for the “type” column
def device_type=(s)
self[:type] = s
end
end
Enjoy!
-Brian
Hi Brian,
That’s a nice solution to the issue, even I was stuck with the same
issue. It solved the inheritence_column issue.
Thanks