Gabe Durazo
Gabe Durazo

Reputation: 1809

Rails - Active Record "RuntimeError: can't modify frozen String" somehow related to form builder?

When I submit the form to update my models I'm getting this error in the browser:

ActiveRecord::StatementInvalid in CoachesController#update

RuntimeError: can't modify frozen String: INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)

The rails console says this:

(0.0ms)  begin transaction
Binary data inserted for `string` type on column `day`
SQL (0.5ms)  INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)  [["coach_id", 14], ["created_at", Mon, 27 Feb 2012 21:59:05 UTC +00:00], ["day", "Monday"], ["hour", 20], ["updated_at", Mon, 27 Feb 2012 21:59:05 UTC +00:00]]
RuntimeError: can't modify frozen String: INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)
(0.1ms)  rollback transaction
Completed 500 Internal Server Error in 25ms

ActiveRecord::StatementInvalid (RuntimeError: can't modify frozen String: INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)):
app/models/coach.rb:93:in `block (2 levels) in update_general_availability'
app/models/coach.rb:92:in `each'
app/models/coach.rb:92:in `block in update_general_availability'
app/models/coach.rb:91:in `each'
app/models/coach.rb:91:in `update_general_availability'
app/controllers/coaches_controller.rb:25:in `update'

After much experimentation I've found how to work around this error, but not why I'm getting it in the first place.

I have two models: Coach and Availabilities, with has_many and belongs_to associations. This is the schema for the availability table:

# Table name: availabilities
#  id         :integer         not null, primary key
#  coach_id   :integer
#  day        :string(255)
#  hour       :integer

It stores a day of the week and hour during the day that a coach is free.

I wrote two methods in the Coach model to more easily deal with a coach's weekly availability. They use a nested hash table, so you can query whether a coach is free at a given time. (E.g.: general_availability["Thursday"]["12"] #=> true )

#coach.rb
class Coach < ActiveRecord::Base
  ...

  # Creates a hash table mapping day and hour to true if available then, false otherwise
  # Form is general_availability["day"]["hr"]. Per Availability model, "0" = midnight, and
  # day of the week is of the form "Monday" or "Tuesday".
  def general_availability
    h = Hash.new()
    %w(Monday Tuesday Wednesday Thursday Friday Saturday Sunday).each { |day| h[day] = Hash.new(false) }

    self.availabilities.each do |a|
      h[a.day][a.hour.to_s] = true
    end

    return h
  end

  # Takes a hash table of the kind returned by general_availability and updates
  # this coach's records in the Availabilities table
  def update_general_availability(ga_hash_table)
    self.availabilities.clear

    ga_hash_table.each do |day, hrs|
      hrs.each do |hr, val|
        self.availabilities.create({day: day, hour: hr.to_i})
      end
    end
  end

This is the partial to display the coach's weekly availability as a table. Each day/hour cell is a checkbox which a coach can check or uncheck to indicate if they're free.

<!-- availabilities/_scheduler.html.erb -->
<h2>General Availability</h2>
Please check the times below that you would generally be available for a training session.
<table class="table" id="availabilities_table">
  <tr>
    <th>Time</th>
    <% days_of_the_week.each do |day| %> 
      <th><%= day %></th> 
    <% end %>
  </tr>
  <% (6..21).each do |hr| %>
    <tr>
      <td><%= format_as_time hr %></td>
      <% days_of_the_week.each do |day| %>
        <% is_checked = @general_availability[day][hr.to_s] %>
        <td class="availabilities_cell">
          <%= check_box_tag "availability[#{day}][#{hr}]", true, is_checked, :class => 'availabilities_check_box' %>
        </td>
      <% end %>
    </tr>
  <% end %>
</table>

This is the controller:

# coaches_controller.rb
...
def edit
  @coach = current_user.coach
  @general_availability = @coach.general_availability
end

def update
  @coach = Coach.find(params[:id])

  @coach.update_attributes(params[:coach])  
  if @coach.save
    @coach.update_general_availability(params[:availability])
    redirect_to @coach
  end
  # ...
end

It is the line

@coach.update_general_availability(params[:availability])

that causes the error.

Now, here is my question. Why does this view cause the above error?

<!-- edit.html.erb version 1 -->
<h1><%= @coach.user.first_name %>  </h1>
<%= form_for @coach, :html => { :multipart => true } do |f| %> 

  <%= f.label :profile_photo %> 
  <%= f.file_field :profile_photo %> 

  <div class="field">
    <%= f.label :phone_number %>
    <%= f.text_field :phone_number %>
  </div>  

  ... More Form Fields Here ...

  <%= render 'availabilities/scheduler' %>
  <%= f.button %> 

<% end %> 

While this view does not?

<!-- edit.html.erb version 2 -->
<h1><%=  @coach.user.first_name %>  </h1>
<%= form_for @coach, :html => { :multipart => true } do |f| %> 

  <%= f.label :profile_photo %> 
  <%= f.file_field :profile_photo %> 

  <div class="field">
    <%= f.label :phone_number %>
    <%= f.text_field :phone_number %>
  </div>  

  ... More Form Fields Here ...

  <%= f.button %> 

<% end %> 

<%= form_for @coach, :class => "form-vertical" do |f| %>
  <%= render 'availabilities/scheduler' %>
  <%= submit_tag "Update Schedule" %>
<% end %>

Note that in the former the partial is inside the form builder form, whereas in the second case the partial is rendered as its own form_for below.

The part that jumps out at me from the log I pasted above is this line:

Binary data inserted for `string` type on column `day`

which does not appear when the form works (e.g., in version 2 of the form). It seems important, but I don't know what it means or why it's happening.

Thanks so much!

Upvotes: 3

Views: 4191

Answers (1)

Gabe Durazo
Gabe Durazo

Reputation: 1809

Figured it out. Ruby hash table keys are frozen. So my params looked like:

params[:availability][:Thursday][:10] = "true"

When my update_general_availability method did this:

self.availabilities.create({day: day, hour: hr.to_i})

:day was "Thursday", but the SQLite Adapter understood it to have Encoding::ASCII_8BIT (aka "binary"), and tried do encode! 'utf-8' it. However, since it was frozen this threw the Runtime Frozen String error. The problem was solved by adding these lines to the update_general_availability method:

day = day.dup
hr = hr.dup

Now, since they're duplicated and not the hash keys themselves, they can get encoded to utf-8.

Upvotes: 2

Related Questions