Class table inheritance problems

I’ve successfully implemented class table inheritance but came across a problem for one instance. I’m using this to simplify my database structure. I have one model that I’m using it for that has no additional properties other than the stuff its inheriting. I’m doing this to keep everything consistent with the architecture. For instance:

[source language=":ruby"]
create_table :products do |table|
table.column :name, :string, :null => false, :limit => ’50′
table.column :description, :text, :null => false
table.column :type, :string, :null => false, :limit => ’50′
table.column :default_photo_id, :int, :limit => ’11′
table.column :user_id, :int, :null => false, :limit => ’11′
end

create_table :anvils do |table|
table.column :product_id, :int, :null => false, :limit => ’11′
end
[/source]

So anvils just inherits all the data from products and doesn’t really add to it. The problem occurs when trying to save a modified anvil record. I end up with an error in the SQL because it’s trying to do something like:

[source language=":sql"]UPDATE anvils SET WHERE product_id = 1[/source]

But there aren’t any local attributes to update on the anvils table. I could redo that one structure to not use class table inheritance but that would cause more of a headache than its worth. I could try to hack a solution using the class table inheritance plugin but that didn’t sound fun either. The easiest solution, and I’m not really proud to admit this, was to simply add a junk tinyint(1) column to the anvils table. That way when the update sql runs it won’t cause a syntax error.

I was looking for a way to do anvil.parent.save or something similar but couldn’t find a way to do that. That’d be a much better solution so if anyone figures that out please let me know.

Comments are closed.