Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:50:27+00:00 2026-05-22T16:50:27+00:00

I have a problem with subclassing Rail’s models. Suppose you have a User model

  • 0

I have a problem with subclassing Rail’s models. Suppose you have a User model and several subclasses of it (user types) that store specific methods and associations, for example: Director, Admin, Trainee, Instructor, etc. This is just simple “single-table inheritance”. The problem is 2-fold.

  1. paths/urls often crash or do weird things when you pass a subclass rather than the base class. Here’s an example:

    <% if user.enabled? %>
      <%= link_to 'Disable', disable_user_path(user) %>
    <% else %>
      <%= link_to 'Enable', enable_user_path(user) %>
    <% end %>
    

    If you pass in a User model, it works just fine. But if you pass a subclass, like Admin, it throws this exception:

    No route matches {:controller=>"users", :action=>"disable", :id=>#<Admin id: 1, first_name: "Ken", ..., created_at: "2011-05-23 21:01:35", updated_at: "2011-05-23 21:04:28">}
    

    Clearly, this is not behaving correctly. How can we get rails to use the base class all the time?

  2. Even more disturbing is forms (I am using simple_form). Let’s say you have a /profile form. You want all User subclasses to access it equally, and you don’t want to deal with their subclasses on a special-case basis; it should be 100% generic.

    For some reason, if the user is an Admin, it will post the params hash as

    params[:admin]
    

    Even worse, if you view the source of the form, it actually says user[first_name] instead of admin[first_name], so something is definitely screwy! The instance variable is @user too, so I don’t see why it should be posting to params[:admin].

Here is the form view code for (2):

<%= simple_form_for(@user, :url => profiles_path, :method => :put, :html => {:multipart => true}) do |f| %>
  <%= f.error_notification %>

  <div class="form">
    <fieldset>
      <legend>Personal Information</legend>
      <%= f.input :first_name %>
      <%= f.input :last_name %>
    </fieldset>

    <fieldset>
      <legend>Credentials</legend>
      <%= f.input :email %>
    </fieldset>

    <fieldset>
      <legend>Preferences</legend>
      <%= f.input :receive_email_notifications %>
      <%= f.input :receive_newsletters %>
      <%= f.input :allow_private_messages %>
    </fieldset>

    <fieldset>
      <legend>Avatar</legend>
      <p>
          Select an image from your computer to use as your avatar. You will be given the oppurtunity
          to crop this image further after your image has been uploaded.
      </p>
      <%= f.input :avatar, :as => :file, :label => "Select File" %>
      <%= f.hidden_field :avatar_cache %>
    </fieldset>

    <div class="actions">
      <%= f.button :submit, :value => "Update Profile" %>
      <%= link_to 'Cancel', profiles_path %>
    </div>
  </div>

<% end %>

Here are the controller actions to first render the view and when I submit the form:

  def edit
    @user = User.find(current_user.id)
  end

  def update
    @user = User.find(current_user.id)

    if @user.update_attributes(params[:user]) # <-- this bombs for Admin subclass
      if params[:user][:avatar] # <-- this would bomb also.
        redirect_to(crop_avatar_profiles_path)
      else
        redirect_to(profiles_path, :notice => 'Your profile was successfully updated.')
      end
    else
      render :action => "edit"
    end
  end

  def crop_avatar
    @user = User.find(current_user.id)
  end

  def update_avatar
    @user = User.find(current_user.id)
    @user.crop(params[:x].to_i, params[:y].to_i, params[:h].to_i, params[:w].to_i)

    redirect_to(profiles_path, :notice => 'Your profile and avatar was successfully updated.')
  end

Besides thinking up some pretty inelegant solutions (especially to the forms problem), I am at a loss as to how I can fix them. There has to be a better way to deal with these situations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:50:27+00:00Added an answer on May 22, 2026 at 4:50 pm

    I had the same problem with STI due to the dynamic casting rails did for me. I solved it using the becomes method.

    In your User model:

        #return an array [User, User, ...], instead of [Admin, Director, Director, Instructor, ...]
        def self.all_without_typecast
          self.all.collect! do |u|
              u.becomes(User)
          end
        end
    
        #makes subclasses of User (Admin, Director, ...) into User class
        def userize #cast_into_user could be a better name
          self.becomes(User)
        end
    

    Now whenever you don’t want a user instance to be of an specific type (Admin, Director..) but just User, you can do:

        @user = @user.userize
    

    And your @user object will now be treated as a User, not Admin or Director or whatever, same goes for all_without_typecast, like this:

        @users = User.all_without_typecast # if you're having problems with User.all
    

    Maybe this methods could be run before each scope so you don’t have to rewrite every one of them.
    Hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with fancybox. I want to write a function that will run
I have a custom NSTableView subclass filled with several custom NSTextFieldCell subclasses. I would
I have an unusual problem. Let's consider such models (taken from django docs): class
I have the same problem as described in the posts listed below. That is,
I am currently on a Problem. I have an RMI Server with a Model
I have built a class that intercepts UIWebView resource loading by subclassing NSURLCache and
have problem with ampersand (&) How to search for the words (or sentences) that
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.