Insert into with only filfull fields

Hello,
when i create a record, User.create(:login=>‘john’, :password =>
‘password’), active record behaviour is to include every of my fields
in the SQL query :
INSERT INTO users (“login”, “password”, “another_column”,
“yet_another_column”) VALUES(‘john’, ‘password’, NULL, NULL)

But i want active record to just include filfulled fields in the
query:
INSERT INTO users (“login”, “password”) VALUES(‘john’, ‘password’)

how can i change it?

Is there any reason you want to do this? This is ActiveRecord’s default
behaviour and changing it would be a pain.

On Jan 9, 2008 11:13 AM, Jean-Sébastien [email protected]
wrote:

INSERT INTO users (“login”, “password”) VALUES(‘john’, ‘password’)

how can i change it?


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

yes my database has some triggers: some fields are “NOT NULL” but
triggers put the good values inside.

You could move the triggers into the model, depending on what they do.

On Jan 9, 2008 11:21 AM, Jean-Sébastien [email protected]
wrote:

wrote:

But i want active record to just include filfulled fields in the
query:
INSERT INTO users (“login”, “password”) VALUES(‘john’, ‘password’)

how can i change it?


Ryan B.http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

Jean-sébastien Jney wrote:

no i can’t: this database is not only acceed my rails processes, we
have some java tasks too.
I think the better way is to modify our triggers.

You could try adding this to your user model,

ignore_column “another_column”
ignore_column “yet_another_column”

after extending ActiveRecord::Base with

class ActiveRecord::Base
def self.ignore_column(column_name)
case column_name
when String, Symbol
self.columns_hash.delete(column_name.to_s)
self.columns.delete_if {|c| c.name == column_name.to_s}
when Regexp
self.columns_hash.delete_if { |k,v| column_name.match(k)}
self.columns.delete_if { |c| column_name.match(c.name)}
end
end
end

Or do you need to read these columns as well?

Stephan

yes i need to read them :(. I already use same kind of hack on other
fields.

On Jan 9, 2:04 am, Stephan W. [email protected]

no i can’t: this database is not only acceed my rails processes, we
have some java tasks too.
I think the better way is to modify our triggers.

Jean-sébastien Jney wrote:

yes i need to read them :(. I already use same kind of hack on other
fields.

On Jan 9, 2:04 am, Stephan W. [email protected]

In that case you can extend the User model with a UserWrite model, which
has those columns hidden. The UserWrite model is only used by the
controller for updates/inserts.

Stephan

ok, i’ll try your solution, thanks everyone for so quick answers

On 9 jan, 03:54, Stephan W. [email protected]