Forum: Ruby on Rails update_attribute does an insert?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matt W. (Guest)
on 2007-07-12 00:59
(Received via mailing list)
Hello,

I am trying to update an integer value on a table using Rails, but it
complains that I am violating a not-null constraint because I didn't
specify other attributes. My code is as follows:

    sup = Supplier.new
    sup.update_attribute('num_products', total_products)

The error I get is:

RuntimeError: ERROR     C23502  Mnull value in column "name" violates
not-null constraint       FexecMain.c     1782   RExecConstraints:
INSERT INTO suppliers("name", "num_products", "drop_ship_fee",
"real_name", "return_policy", shipping_terms", "url", "description")
VALUES(NULL, 161, 400, NULL, NULL, NULL, NULL, NULL)

Why is it doing an insert instead of just an update on the single
attribute I want to change? Thanks!
Philip H. (Guest)
on 2007-07-12 01:09
(Received via mailing list)
> I am trying to update an integer value on a table using Rails, but it
> complains that I am violating a not-null constraint because I didn't
> specify other attributes. My code is as follows:
>
>    sup = Supplier.new
>    sup.update_attribute('num_products', total_products)

You are creating a *new* Supplier in 'sup'.  So there's no associated
record in the database for you to update the 'num_products' column.

Try something like:

sup = Supplier.find(123)
sup.update_attribute('num_products', total_products)

-philip
Rob B. (Guest)
on 2007-07-12 01:43
(Received via mailing list)
On Jul 11, 2007, at 5:23 PM, Philip H. wrote:
> Try something like:
>> INSERT INTO suppliers("name", "num_products", "drop_ship_fee",
>> "real_name", "return_policy", shipping_terms", "url", "description")
>> VALUES(NULL, 161, 400, NULL, NULL, NULL, NULL, NULL)
>>
>> Why is it doing an insert instead of just an update on the single
>> attribute I want to change? Thanks!

If that's really what you want to do, there are a couple other ways
to go about it:

   sup = Supplier.new(:num_products => total_products)

   sup = Supplier.new do |s|
     s.num_products = total_products
   end

or if you want it to be on the disk NOW! you might want something like:

   sup = Supplier.create(:name => "Supplier One", :num_products =>
total_products)

or check out the find_or_create_by_* methods (or even
find_or_initialize_by_*) if you might have a record, but are sure you
will have one by the time the call is finished.

   sup = Supplier.find_or_initialize_by_name("Supplier One") do |s|
     s.num_products = total_products
     # .. other changes
   end

   sup.save    # writes the database once

-Rob

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
This topic is locked and can not be replied to.