# Passing an SQL fragment in AR save

**URL:** <https://www.ruby-forum.com/t/passing-an-sql-fragment-in-ar-save/161904>\
**Category:** Rails\
**Created:** [March 3, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/passing-an-sql-fragment-in-ar-save/161904 "2009-03-03T17:41:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 3, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/passing-an-sql-fragment-in-ar-save/161904/1 "2009-03-03T17:41:42Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 3, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/passing-an-sql-fragment-in-ar-save/161904/2 "2009-03-03T17:58:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 3, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/passing-an-sql-fragment-in-ar-save/161904/3 "2009-03-03T20:07:20Z")

</div>

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?
