21 Feb 2009, 19:21
Generic-user-small

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?
Thanks.

15 Mar 2009, 18:51
Generic-user-small

Steve Downie (1 post)

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

I’ve never come across this error before.

Thanks,

– Steve

20 Mar 2009, 03:25
Ryan_bates_cropped_pragsmall

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 > ?", Date.today, Date.today])
end

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 > ?", Date.today, Date.today])
end

Hope that helps.

Ryan

28 Aug 2009, 16:13
Cyril_pragsmall

Cyril (1 post)

Hello,

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 > ?", Date.today, Date.today])
end
03 Aug 2010, 19:34
N27304533_26753_pragsmall

David Blankenship (6 posts)

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

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

@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