drewwyatt
drewwyatt

Reputation: 6027

Do I need to update an attribute in rails? (and how?)

I generated scaffolding for a class: ExpenseReport One of the attributes was type - which I created a selct for, where two options where possible: Major and Regular.

When submitting the form to create a report, I was pulling this error:

Invalid single-table inheritance type: Regular is not a subclass of ExpenseReport

My assumption was - "Okay, let's just not have an attribute called type, it looks like that may be causing problems." So I created a migration, renaming type to "report type" and raked (see migration and proof of rake below)

Migration

class UpdaeColumnName < ActiveRecord::Migration
  def change
     rename_column :expense_reports, :type, :report_type
  end
end

Proof of Rake

Drews-MacBook-Pro:depot drewwyatt$ rails generate migration UpdaeColumnName
      invoke  active_record
      create    db/migrate/20130820215925_updae_column_name.rb
Drews-MacBook-Pro:depot drewwyatt$ rake db:migrate
==  UpdaeColumnName: migrating ================================================
-- rename_column(:expense_reports, :type, :report_type)
   -> 0.0021s
==  UpdaeColumnName: migrated (0.0021s) =======================================

Now, however, it never ever saves my input, and fires validation with every submit - telling me "Report type is not included in the list", is there another attribute name I need to update or something?

Relevant _form.html.erb

<div class="field">
    <%= f.label :report_type %><br>
    <%= f.select :report_type, ExpenseReport::TYPES,
                        prompt: "select one" %>
  </div>

Model

class ExpenseReport < ActiveRecord::Base
    validates :place_of_purchase, :items, :reason, :estimated_cost, 
              :requestor_name, presence: true
    TYPES =   [ 'Major', 'Regular' ]
    validates :report_type, inclusion: TYPES
    SITES = [ '001 (Lubbock)', '002 (Odessa)', '003 (Midland)', '004 (Lubbock)' ]
    validates :site, inclusion: SITES
end

Upvotes: 0

Views: 91

Answers (1)

Matthias
Matthias

Reputation: 4375

The attribute "type" is for single table inheritance, more about can be found here: http://railscasts.com/episodes/394-sti-and-polymorphic-associations or here: http://rails-bestpractices.com/posts/45-use-sti-and-polymorphic-model-for-multiple-uploads

If you stay with your new report_type, you should change your scaffold stuff. Are you on rails 4? If yes, change your private expense_report_params method in your expense_reports_controller.rb

Should be something like that:

def expense_report_params
  params.require(:expense_report).permit(:place_of_purchase, :items, :reason, :estimated_cost, :requestor_name, :other_attributes, :type)
end

Change it to:

def expense_report_params
  params.require(:expense_report).permit(:place_of_purchase, :items, :reason, :estimated_cost, :requestor_name, :other_attributes, :report_type)
end

In rails 4 you always have to permit your params..otherwise it would not work. If you permit a params "type" which does not exist, you´ll get an error..

Upvotes: 1

Related Questions