Navin Samuel

ActiveScaffold & STI (Single Table Inheritance)

In Technology, Tecnoquips on July 21, 2008 at 11:53 am

I had a tough time getting ActiveScaffold to work with STI.  As per the development roadmap for AS, this will be available only in version 1.3! Since I desperately needed this to work, I figured out a quick workaround. In the models, rename your foreign keys to the name of the derived class. That’s it! Below is an example.


# Base class
class Promotion < ActiveRecord::Base
end


# Derived class
class SurveyPromotion < Promotion
has_many :surveys,
:foreign_key => 'survey_promotion_id'
end


# Associated class
class Survey < ActiveRecord::Base
belongs_to :survey_promotion,
:foreign_key => 'survey_promotion_id'
end

  1. I too was struggling with the same issue, but it’s dependent upon Type column as rails STI, which is a protected column and I wasn’t able to set/update this attribute.

    Solution:
    Create/Update and EMBEDDING :
    * constraints are now applied to a record during the #new action, in case any custom form rendering depends on the constrained column

    So i just rendered my action based on :constraints => { :type => ‘parent_model’} and it worked and its a very good fix by AS.

    Thanks,
    Akshay

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.