Mass Assigning Inheritance Column Values for ActiveRecord STI with Rails
One of the security features in Rails is to prevent mass assigning values for certain columns (when passing a hash to #new, #attributes=, and #update_attributes). This is to ensure that a malicious user can't exploit the system by passing in values for certain attributes you really don't want them to change.
In Rails 2.1, one of the attributes that will always be protected from mass assignment is the inheritance_column. In most cases, protecting inheritance_column; is desired because we don't want (for example) someone to change their user type by specifying type = 'Admin' when posting a form. Despite this, there are some cases where we do want to allow the user to specify the type.
However, even if we specify #attr_accessible on the inheritance_column, we will not be able to mass assign a value to it. Rails will not allow it unless we put in a little hack to work around this feature.
Let's start with an example; say we have an STI relationship like the following:
class Animal < ActiveRecord::Base; end class Dog < Animal; end class Cat < Animal; end class Smurf < Animal; end
We have a form where the user enters in some basic information about their animal and chooses the type they want. We don't want to limit what type of animal the user can choose, so we want to allow them to set the type themselves. Instead of explicitly setting the type separately from any mass assignment, we can override Rails' protection for the Animal class.
class Animal < ActiveRecord::Base
private
def attributes_protected_by_default
default = super
default.delete self.class.inheritance_column
default
end
end
What this code does is override the method which Rails uses to specify which columns should never be mass assignable. We get Rails' array of default protected attributes, and just remove the inheritance_column from the array. Now, we can pass the type into all the mass assigning methods and not have to set it separately.
Topics: activerecord, Ruby on Rails, STI
Comments: 3 so far
Leave a comment
About Pathfinder
Follow the Blog
-
Get a monthly update on best practices for delivering successful software.
Subscribe via email
Subscribe via RSS
Categories
Topics
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
Blogroll
Recent
- Elements of Testing Style
- Aesthetics and Web Design
- Asterisk-Java Testing with Groovy
- 3 Misuses of Code Comments
- Fluently NHibernate
- Digging a Hole and Covering it with Leaves — The Software Development Version
- The Importance of User Experience - Do You Understand It in Your Bones?
- Writing Your Own Protocol With NSURLProtocol
- What’s In Your Dock: iPhone edition
- Feature Fatigue


Anthony, thanks so much. This solved my exact problem very neatly. I am using this with the StreamLined framework.
Comment by Mike Berrow, Monday, November 3, 2008 @ 4:53 am
Thanks a lot, I was looking exactly for that
Comment by Jacek Becela, Friday, December 5, 2008 @ 7:38 am
Woo hoo, worked like a charm! Thanks!
Comment by Dan, Monday, March 9, 2009 @ 2:38 pm