Passing an SQL fragment in AR save


#1

I have a basic model class TestTime with no customization.

class TestTime < ActiveRecord::Base
end

I would like to pass the following SQL fragment as part of an AR save
call to TestTime objects. How and where would I do this in the model?

“set c_time=current_time”

(current_time is an SQL method, not a variable).


#2

On 3 Mar 2009, at 16:41, James B. wrote:

I have a basic model class TestTime with no customization.

class TestTime < ActiveRecord::Base
end

I would like to pass the following SQL fragment as part of an AR save
call to TestTime objects. How and where would I do this in the model?

Nothing builtin for that as far as I know. If you dig around in the AR
source you may find a suitable method to overide/piggyback on

Fred


#3

Frederick C. wrote:

On 3 Mar 2009, at 16:41, James B. wrote:

I would like to pass the following SQL fragment as part of an AR save
call to TestTime objects. How and where would I do this in the model?

Nothing builtin for that as far as I know. If you dig around in the AR
source you may find a suitable method to overide/piggyback on

In AR::Base I find this:

  def update(attribute_names = @attributes.keys)
    quoted_attributes = attributes_with_quotes(false, false, 

attribute_names)
return 0 if quoted_attributes.empty?
connection.update(
"UPDATE #{self.class.quoted_table_name} " +
"SET #{quoted_comma_pair_list(connection, quoted_attributes)}
" +
“WHERE #{connection.quote_column_name(self.class.primary_key)}
= #{quote_value(id)}”,
“#{self.class.name} Update”
)
end

So, I think that something along the lines of:

module HLLARUpdateSQL
def self.included(base)

  def update_sql(id,fragment)
    return 0 unless fragment
    connection.update(
      "UPDATE #{self.class.quoted_table_name} " +
      "SET #{fragment} " +
      "WHERE #{connection.quote_column_name(self.class.primary_key)} 

" +
“#{quote_value(id)}”,
“#{self.class.name} Update”
)

  end

end
end

might serve. Your thoughts?