Question about ActiveRecord

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

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

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

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

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

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

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

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