Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

Sandup for 08/27/2009: Single Table Inheritance and the becomes method

Interesting Things

  • Be careful when extending classes from structs. Their superclasses are essentially anonymous classes, so reopening them can be difficult. If you attempt to reopen them by extending them from “the same” struct, it will actually be a different anonymous class.
  • Ever had an STI model but wanted the views and controllers to pretend like it all extended the base class? You can have rails change the params[] namespace that it uses like so:

    form_for :user, @admin_user, :url => user_path(@admin_user)

Or you can be super-cool and use polymorphic routes:

form_for @admin_user.becomes(User)

The becomes method is part of ActiveRecord, and it actually creates a 2nd copy of the object with the same attributes and a different class (shallow copy). Due to this implementation, it has limitations so use it carefully.

  • Upgrading to rails 2.3.3 breaks HopToad. This is related to filter_parameter_logging, and it’s technically rails’ fault. It has been fixed in 2.3.3 stable (which I assume will be released as 2.3.4). You can also fix it yourself with a one-line-patch. Personally, I’d wait to upgrade till 2.3.4 comes out.

  1. Kelly Felkins says:

    > Due to this implementation,
    > it has limitations _so use it carefully_.

    Often the example given:

    form_for @admin_user.becomes(User)

    is something like

    form_for @admin_user.becomes(User) do |f|

    When `becomes` makes its shallow copy, it **does not** copy the errors attribute, so the error messages will never be displayed.

    Another solution is to just be more explicit in your form_for statement:

    form_for :user, @admin_user, :url => users_path, :html => {:method => :post} do |f|

    I haven’t decided if the lack of errors in the new object created by becomes is a bug.


Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *