
Originally Posted by
mwmitchell
OK cool. But just for my own knowledge, how do you set a models values directly? Without quoting?
From outside the model? Don't know, sorry.
From inside the model, write_attribute/read_attribute are useful, but everything in the attributes hash goes through attributes_with_quotes on create and update (called by my_model_object.save). That means you'll have to write the SQL yourself to get unquoted data in there.
Here's how Rails does update internally, you could base your code on that:
Code:
1435 def update
1436 connection.update(
1437 "UPDATE #{self.class.table_name} " +
1438 "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " +
1439 "WHERE #{self.class.primary_key} = #{quote(id)}",
1440 "#{self.class.name} Update"
1441 )
1442 end
So you'd have something like:
Code:
def custom_update
custom_sql_data = "... now() ..."
connection.update(
"UPDATE #{self.class.table_name} " +
"SET #{custom_sql_data} " +
"WHERE #{self.class.primary_key} = #{quote(id)}",
"#{self.class.name} Update"
)
end
And I think it should work. Just remember that you are going around most of ActiveRecord by doing that, so if you find yourself doing it regularly, you might want to use a different ORM, or just not bother with an ORM in the first place 
Douglas
Bookmarks