small medium large xlarge

21 Feb 2009, 19:21
Terry Brown (1 post)

After populating the database using rake db:populate on the code from the 5th screencast, and reloading the server, I get a ActiveRecord::ReadOnlyRecord error when reloading the home page.

ActiveRecord::ReadOnlyRecord in Movies#index

Showing app/views/movies/index.html.erb where line #9 raised:

ActiveRecord::ReadOnlyRecord Extracted source (around line #9):

6: <div class="minutes">Run Time: <%= movie.minutes %> minutes</div> 7:
8: <div class="reviews"> 9: <%= display_stars(movie.average_stars) %> 10: </div> 11: </div> 12: <% end %>

How do I fix this?

15 Mar 2009, 18:51
Steve Downie (1 post)

I just came across the same issue. Was this ever resolved?

I’ve never come across this error before.


– Steve

20 Mar 2009, 03:25
Ryan Bates (60 posts)

Hi Terry and Steve,

Sorry for not responding about this sooner. It seems records fetched with a :joins clause are now read only. There are a couple ways to get around this. One is to pass in a :select clause which seems to return writable records:

def index
  @movies = Movie.find(:all, :select => "movies.*", :joins => :showings, :conditions =>
              ["showings.starts_on < ? AND showings.ends_on > ?",,])

Another is to use :include instead of :joins. However this will probably not perform as well.

def index
  @movies = Movie.find(:all, :include => :showings, :conditions =>
              ["showings.starts_on < ? AND showings.ends_on > ?",,])

Hope that helps.


28 Aug 2009, 16:13
Cyril (1 post)


An other way is to disable readonly like so:

def index
  @movies = Movie.find(:all, :readonly => false, :joins => :showings, :conditions =>
              ["showings.starts_on < ? AND showings.ends_on > ?",,])
03 Aug 2010, 19:34
David Blankenship (6 posts)

I seem to be having a similar problem using Rails3 RC:

def update
  @event =[:id], :readonly => false)
  puts @event.readonly?
  if @event.update_attributes(params[:event])
    flash[:notice] = "Updated."

@event seems to still be readonly, even thought I specified it to not be read only on the find. I thought maybe it was current_user which was readonly, so I even placed readonly=>false in its original find. Ryan, you are right, if I use :select => “events.*” it is no longer read only. Just doesn’t look as nice to me. :D Still, better than it not working.

  You must be logged in to comment