How do I change :type attribute in a view?

I’m using single table inheritance and want to change the :type
attribute from an admin view. How do I do this given that :type has to
be handled with special care?

Jose Hales-garcia wrote:

I’m using single table inheritance and want to change the :type
attribute from an admin view. How do I do this given that :type has to
be handled with special care?

type is protected from mass assignment so you have to do something like:

@model = Model.find(params[:id])
@model[:type] = params[:model][:type]
@model.update_attributes(params[:model])


We develop, watch us RoR, in numbers too big to ignore.

Mark Reginald J. wrote:

Jose Hales-garcia wrote:

I’m using single table inheritance and want to change the :type
attribute from an admin view. How do I do this given that :type has to
be handled with special care?

type is protected from mass assignment so you have to do something like:

@model = Model.find(params[:id])
@model[:type] = params[:model][:type]
@model.update_attributes(params[:model])

Thank you on the advice for the controller code. But I’m getting an
error in the view (see below). Are there special techniques for
handling the :type attribute inside of a view?

wrong argument type String (expected Module)
Extracted source (around line #12):

9:


10:
11:
12: <%= select :document, :type,
ApplicationHelper::DOCUMENT_TYPES %>
13:
14:
15:

Jose Hales-Garcia wrote:

Thank you on the advice for the controller code. But I’m getting an
error in the view (see below). Are there special techniques for
handling the :type attribute inside of a view?

wrong argument type String (expected Module)
Extracted source (around line #12):

12:

<%= select :document, :type, ApplicationHelper::DOCUMENT_TYPES %>

Yes, it’ll be using the Ruby built-in type method rather than the
AR type attribute method because the latter uses method_missing.

You’ll have to do one of three things:

  1. Change to a different attribute name using set_inheritance_column
  2. Add a method to the document model so that type can be accessed under
    a different method name
  3. Code as:
    select_tag ‘document[type]’,
    options_for_select(ApplicationHelper::DOCUMENT_TYPES,
    @document[:type])


We develop, watch us RoR, in numbers too big to ignore.