Red Squirrel Stumblog RSS

Dave Hoover stumbles through technology

Dec
31st
Mon
permalink

dup vs. clone

You might think that ActiveRecord::Base#dup is what you want, but you really want clone.
Dec
9th
Sun
permalink

ActiveRecord's QueryCache#uncache

Sometimes you need to compare an attribute of an in-memory ActiveRecord instance against its attribute in the database. The way I usually do that is to grab a fresh instance from the database like this:

class Something < ActiveRecord::Base
  def previous(attribute)
    self.class.find(id)[attribute]
  end
end

That worked just fine in Rails 1.2. But Rails 2.0 has request-scoped query caching enabled by default, meaning that if you have already executed the same SQL that is generated by find(id), you’re going to get the exact same instance back from that call to find. This makes it impossible to compare in-memory attributes against attributes stored in the database.

The solution is to use ActiveRecord’s uncache method. This is a class method, so you’ll need to grab your class to call it like this:

class Something < ActiveRecord::Base
  def previous(attribute)
    self.class.uncached { self.class.find(id)[attribute] }
  end
end
Dec
3rd
Mon
permalink

finder_sql String gets eval'd later

If you ever need to use ActiveRecord’s has_many with finder_sql, remember that the key to finder_sql is that the String get eval’d in the context of the ActiveRecord instance rather than the ActiveRecord class. So make sure you use single quotes! This prevents the String from being interpolated too soon.

Do this:
has_many :foos,
  :finder_sql => 'select ... where some_id=#{id}'
Not this:
has_many :foos,
  :finder_sql => "select ... where some_id=#{id}"
In the former case, the #{id} will be the id of the ActiveRecord instance. In the latter case, the #{id} will be the id of the ActiveRecord class.
Nov
16th
Fri
permalink

Watch out for ISO-8859-1 on Rails

Rails uses a UTF-8 charset by default. If you have latin1 on the backend, you’ll end up with a questions mark (yes, a “?”) instead of a ® on the front end. Here’s one way to fix it, but be sure to heed Brian’s comment. 
Nov
14th
Wed
permalink

Do NOT require Rails models in Rails unit tests

When the test runs, it will load the class twice.  Loading the class twice means that all of the model’s lifecyle hooks will get called twice.
Oct
24th
Wed
permalink

InPlaceEditor loadTextURL changed to GET

In the latest Scriptaculous InPlaceEditor, the loadTextURL will use a GET request. If you’re on Rails and using RESTful Resources for that action, make sure to update your routes.rb from :post to :get.
Oct
10th
Wed
permalink

MySQL gem: NSLinkModule() error

If you’re getting this error when you try to connect to MySQL through Ruby, follow these instructions.
Oct
2nd
Tue
permalink

ActiveRecord predicate method

Keep in mind that you can use a predicate method to access your boolean attributes in the Ruby way.
  placement = ImagePlacement.create!(:ignored => true)
  placement.ignored?  # true
Sep
27th
Thu
permalink

update_attribute vs. update_attributes

When do you use one or the other? (Hint: the answer does not depend on whether you want to update one or many attributes.) The most important difference between these methods is that update_attribute bypasses validations.
permalink

Unsettable ActiveRecord::Base attribute

If your ActiveRecord is ignoring your attribute, make sure to check that your model class isn’t restricting which attributes are accessible. Like, say, if you’re using the restful_authentication plugin…
  attr_accessible :login, :email, :password, :pass...
This method is a nice way to lock down which attributes can be changed in an ActiveRecord, but it can be maddening when you’re trying to add a new attribute and you didn’t notice the attr_accessible declaration.