# Question about ActiveRecord

**URL:** <https://www.ruby-forum.com/t/question-about-activerecord/103786>\
**Category:** Rails\
**Created:** [May 10, 2007, 10:41am UTC](https://www.ruby-forum.com/t/question-about-activerecord/103786 "2007-05-10T10:41:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ce7236/32.png) [@laurent](https://www.ruby-forum.com/u/laurent)\
**Post date:** [May 10, 2007, 10:41am UTC](https://www.ruby-forum.com/t/question-about-activerecord/103786/1 "2007-05-10T10:41:42Z")

</div>

Hi

I have a quick creation form (in a form) : the displayed fields  
correspond only to some of the attributes of the corresponding model.

In my controller , create action :  
esaily, i would like to use the :  
@user = User.new(params[:user])  
if @user.save  
#success  
else  
#failure  
end

But i would like to know if the generated SQL statement for creation  
will include all the columns of the mapped table by my model (i think  
yes…)

Is there a tip to only save fields that are not empty for example or  
better, only certain named fields:

The table has many columns : col1, col2, col3, col4, …, colN

I’d like to have an INSERT statement like this one :  
INSERT INTO Users(col1, col2, col3, col4) VALUES (‘a’, ‘b’, ‘c’, ‘d’)  
– Others columns : col5…colN are not concerned by this INSERT

Thanks

Laurent

---

<div class="post-metadata">

**Author:** ![laurent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ce7236/32.png) [@laurent](https://www.ruby-forum.com/u/laurent)\
**Post date:** [May 10, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/question-about-activerecord/103786/2 "2007-05-10T13:06:58Z")

</div>

Laurent,

Next to what Pat wrote, you should take the habit of looking at the  
/log/developement.log  
file.  
You’ll see there 1/the parameters passed to the action, and 2/ the  
executed SQL commands.  
The logs are an invaluable resource.

## Alain

[blog.ravet.com](http://blog.ravet.com)

---

<div class="post-metadata">

**Author:** ![laurent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ce7236/32.png) [@laurent](https://www.ruby-forum.com/u/laurent)\
**Post date:** [May 10, 2007, 10:52am UTC](https://www.ruby-forum.com/t/question-about-activerecord/103786/3 "2007-05-10T10:52:43Z")

</div>

On 5/10/07, Laurent [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #success
> 
> >

If you only pass columns col1, col2, col3, and col4 to  
update\_attributes, then those are the only ones that are going to be  
updated in the db.

Pat

---

<div class="post-metadata">

**Author:** ![laurent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ce7236/32.png) [@laurent](https://www.ruby-forum.com/u/laurent)\
**Post date:** [May 11, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/question-about-activerecord/103786/4 "2007-05-11T17:07:43Z")

</div>

Laurent wrote:

> Then , the SQL statement (INSERT) i see, includes all the columns…too  
> bad.  
> …  
> In Oracle, with views on top of a single Table, you can do  
> Inserts…But, this insert will fail on virtual columns.  
> That’s why i need to filter the columns in my INSERT statement…

Check out the attr\_readonly patch:  
[http://dev.rubyonrails.org/ticket/6896](http://dev.rubyonrails.org/ticket/6896)

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![laurent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ce7236/32.png) [@laurent](https://www.ruby-forum.com/u/laurent)\
**Post date:** [May 11, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/question-about-activerecord/103786/5 "2007-05-11T17:49:48Z")

</div>

Mark,

Are you sure it answers my issue…  
Because i see on the page for your Patch, it’s related to  
counter\_cache…

[PATCH] counter\_cache value gets overwritten

Laurent

---

<div class="post-metadata">

**Author:** ![laurent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ce7236/32.png) [@laurent](https://www.ruby-forum.com/u/laurent)\
**Post date:** [May 10, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/question-about-activerecord/103786/6 "2007-05-10T16:33:28Z")

</div>

Yes Alain, it is what i do since i began to develop (with RoR, or as  
for more longer time with Java ) 😉

When checking the logs i see the generated SQL statement…

I do in my controller :  
suivi = Vgisuivi.new do |s|  
s.update\_attribute(‘id’, @sequence.to\_i)  
s.update\_attribute(‘role\_id’, params[:role\_id])  
s.update\_attribute(‘objet\_id’, params[:objet\_id])  
s.update\_attribute(‘user\_id’ , params[:user\_id])  
end

Then , teh SQL statement (INSET) i see, includes all the columns…too  
bad.

In fact i need to do this because  
We work on a legacy database …in Oracle  
On top of the tables we do some views, more closer to RoR conventions  
(naming pk by id, etc etc…)  
Some of these views have virtual columns (i.e the label of a code,  
coming from the call of a PL/SQL function)

In Oracle, with views on top of a single Table, you can do  
Inserts…But, this insert will fail on virtual columns.  
That’s why i need to filter the columns in my INSERT statement…

L

---

<div class="post-metadata">

**Author:** ![laurent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ce7236/32.png) [@laurent](https://www.ruby-forum.com/u/laurent)\
**Post date:** [May 11, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/question-about-activerecord/103786/7 "2007-05-11T18:36:52Z")

</div>

Laurent wrote:

> Are you sure it answers my issue…  
> Because i see on the page for your Patch, it’s related to  
> counter\_cache…
> 
> [PATCH] counter\_cache value gets overwritten

The attr\_readonly patch is a general solution for restricting  
the fields included in database updates, but all fields are still  
included in database inserts (ActiveRecord new\_record? = true).  
So it may not be a total solution to your problem.

Mark

–  
We develop, watch us RoR, in numbers too big to ignore.
