Friday, July 23, 2010

Almost Switched to DataMapper from ActiveRecord

I was doing what seemed to be a straight-forward call with ActiveRecord from within the update method of a controller:

@project = current_person.projects.find(params[:id])


Well, when doing so I would get an error:

ActiveRecord::ReadOnlyRecord in ProjectsController#update


I was so frustrated by trying to workaround this issue that I spent the morning researching and playing with DataMapper with the intention of switching.

However, after feeling underwhelmed by the current state of DataMapper on Rails 3, I went back to finding a workaround. Luckily I found this comment at the bottom of a Pragmatic Bookshelf thread:

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


That did the trick. Why :readonly => false it not the default, I do not understand.

2 comments:

  1. What problems did you have with DataMapper on Rails 3? It should work flawlessly given the dm-rails plugin.

    ReplyDelete
  2. sorry i missed this comment earlier. i still might move to DataMapper. at the time i ran into a few issues (don't recall what they were) and just didn't have the will to get it installed and ensure DM worked with the plugins I'm using.

    ReplyDelete