On Chapter 7 I have an mysql error in the development environment.

1 view
Skip to first unread message

dwaonsolo

unread,
May 1, 2009, 4:29:03 PM5/1/09
to Beginning Ruby on Rails E-Commerce
I am new to rails so can some one give me a step by step guide on how
to fix this please, i am using ubuntu. and when i press enter i am
confronted with tis error.

>> product = Product.create(:title => 'A Product', :area => area, :sellers => [seller], :bought_at => Time.now, :type => 'Book', :description => 'The description', :new_old => 'old', :price => 30.5)
ActiveRecord::StatementInvalid: Mysql::Error: Column 'area_id' cannot
be null: INSERT INTO products (`price`, `created_at`, `new_old`,
`product_image`, `title`, `updated_at`, `area_id`, `type`,
`bought_at`, `description`) VALUES(30.5, '2009-05-01 21:05:39', 'old',
NULL, 'A Product', '2009-05-01 21:05:39', NULL, NULL, '2009-05-01
21:05:37', 'The description')
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/connection_adapters/abstract_adapter.rb:128:in `log'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/connection_adapters/mysql_adapter.rb:243:in `execute'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/connection_adapters/mysql_adapter.rb:253:in `insert'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/base.rb:1799:in `create_without_callbacks'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/callbacks.rb:254:in `create_without_timestamps'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/timestamp.rb:39:in `create'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/base.rb:1777:in `create_or_update_without_callbacks'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/callbacks.rb:242:in `create_or_update'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/base.rb:1533:in `save_without_validation'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/validations.rb:752:in `save_without_transactions'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/transactions.rb:129:in `save'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/connection_adapters/abstract/database_statements.rb:
59:in `transaction'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/transactions.rb:95:in `transaction'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/transactions.rb:121:in `transaction'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/transactions.rb:129:in `save'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/base.rb:448:in `create'
from (irb):3>>

how do i fix this?

Jarkko Laine

unread,
May 1, 2009, 4:41:11 PM5/1/09
to railsec...@googlegroups.com
On 1.5.2009, at 23.29, dwaonsolo wrote:
> I am new to rails so can some one give me a step by step guide on how
> to fix this please, i am using ubuntu. and when i press enter i am
> confronted with tis error.
>
>>> product = Product.create(:title => 'A Product', :area =>
>>> area, :sellers => [seller], :bought_at => Time.now, :type =>
>>> 'Book', :description => 'The description', :new_old =>
>>> 'old', :price => 30.5)
> ActiveRecord::StatementInvalid: Mysql::Error: Column 'area_id' cannot
> be null: INSERT INTO products (`price`, `created_at`, `new_old`,
> `product_image`, `title`, `updated_at`, `area_id`, `type`,
> `bought_at`, `description`) VALUES(30.5, '2009-05-01 21:05:39', 'old',
> NULL, 'A Product', '2009-05-01 21:05:39', NULL, NULL, '2009-05-01
> 21:05:37', 'The description')

The error says it all. You're not setting the value of area_id, which
is a NOT NULL column in your database.

Add either :area_id => some_valid_area_id into the create call or set
product.area before saving it.

//jarkko
--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi

dwaonsolo

unread,
May 1, 2009, 5:46:57 PM5/1/09
to Beginning Ruby on Rails E-Commerce

> >>> product = Product.create(:title => 'A Product', :area =>  
> >>> area, :sellers => [seller], :bought_at => Time.now, :type =>  
> >>> 'Book', :description => 'The description', :new_old =>  
> >>> 'old', :price => 30.5)
> > ActiveRecord::StatementInvalid: Mysql::Error: Column 'area_id' cannot
> > be null: INSERT INTO products (`price`, `created_at`, `new_old`,
> > `product_image`, `title`, `updated_at`, `area_id`, `type`,
> > `bought_at`, `description`) VALUES(30.5, '2009-05-01 21:05:39', 'old',
> > NULL, 'A Product', '2009-05-01 21:05:39', NULL, NULL, '2009-05-01
> > 21:05:37', 'The description')
>
> The error says it all. You're not setting the value of area_id, which  
> is a NOT NULL column in your database.
>
> Add either :area_id => some_valid_area_id into the create call or set  
> product.area before saving it.



This the code i put in.

product = Product.create(
:title => 'A Product',
:area => 'area',
:sellers => [seller],
:bought_at => Time.now,
:type => 'Book',
:description => 'The description',
:new_old => 'old',
:price => 30.5
)

where would i insert that code in?

Jarkko Laine

unread,
May 2, 2009, 10:10:30 AM5/2/09
to railsec...@googlegroups.com


Sent from my iPhone
Oh, you already had :area => area in your original code. That should
be enough so I'd suspect that area is nil.

In your second message you're setting area to string 'area' which of
course won't work.

//Jarkko

>
>
> >

dwaonsolo

unread,
May 3, 2009, 8:29:42 AM5/3/09
to Beginning Ruby on Rails E-Commerce


On May 2, 3:10 pm, Jarkko Laine <jarks...@gmail.com> wrote:
> Sent from my iPhone
>
> On 2.5.2009, at 0.46, dwaonsolo <nort...@hotmail.co.uk> wrote:
>
>
>
>
>
> >>>>> product = Product.create(:title => 'A Product', :area =>
> >>>>> area, :sellers => [seller], :bought_at => Time.now, :type =>
> >>>>> 'Book', :description => 'The description', :new_old =>
> >>>>> 'old', :price => 30.5)
> >>> ActiveRecord::StatementInvalid: Mysql::Error: Column 'area_id'  
> >>> cannot
> >>> be null: INSERT INTO products (`price`, `created_at`, `new_old`,
> >>> `product_image`, `title`, `updated_at`, `area_id`, `type`,
> >>> `bought_at`, `description`) VALUES(30.5, '2009-05-01 21:05:39',  
> >>> 'old',
> >>> NULL, 'A Product', '2009-05-01 21:05:39', NULL, NULL, '2009-05-01
> >>> 21:05:37', 'The description')
>
> >> The error says it all. You're not setting the value of area_id, which
> >> is a NOT NULL column in your database.
>
> >> Add either :area_id => some_valid_area_id into the create call or set
> >> product.area before saving it.
>
> > This the code i put in.
>
> > product = Product.create(
> > :title => 'A Product',
> >
> > :sellers => [seller],
> > :bought_at => Time.now,
> > :type => 'Book',
> > :description => 'The description',
> > :new_old => 'old',
> > :price => 30.5
> > )
>
> > where  would i insert that code in?
>


So should i turn

:area => 'area'

into

:area => '1'

dwaonsolo

unread,
May 3, 2009, 8:32:52 AM5/3/09
to Beginning Ruby on Rails E-Commerce


On May 2, 3:10 pm, Jarkko Laine <jarks...@gmail.com> wrote:
> Sent from my iPhone
>
> On 2.5.2009, at 0.46, dwaonsolo <nort...@hotmail.co.uk> wrote:
>
>
>
>
>
> >>>>> product = Product.create(:title => 'A Product', :area =>
> >>>>> area, :sellers => [seller], :bought_at => Time.now, :type =>
> >>>>> 'Book', :description => 'The description', :new_old =>
> >>>>> 'old', :price => 30.5)
> >>> ActiveRecord::StatementInvalid: Mysql::Error: Column 'area_id'  
> >>> cannot
> >>> be null: INSERT INTO products (`price`, `created_at`, `new_old`,
> >>> `product_image`, `title`, `updated_at`, `area_id`, `type`,
> >>> `bought_at`, `description`) VALUES(30.5, '2009-05-01 21:05:39',  
> >>> 'old',
> >>> NULL, 'A Product', '2009-05-01 21:05:39', NULL, NULL, '2009-05-01
> >>> 21:05:37', 'The description')
>
> >> The error says it all. You're not setting the value of area_id, which
> >> is a NOT NULL column in your database.
>
> >> Add either :area_id => some_valid_area_id into the create call or set
> >> product.area before saving it.
>
> > This the code i put in.
>
> > product = Product.create(
> > :title => 'A Product',
> >
> > :sellers => [seller],
> > :bought_at => Time.now,
> > :type => 'Book',
> > :description => 'The description',
> > :new_old => 'old',
> > :price => 30.5
> > )
>
> > where  would i insert that code in?
>


Message has been deleted

Jarkko Laine

unread,
May 3, 2009, 11:23:40 AM5/3/09
to railsec...@googlegroups.com

On 3.5.2009, at 16.20, dwaonsolo wrote:

>
>
>
> On May 2, 3:10 pm, Jarkko Laine <jarks...@gmail.com> wrote:

>> Sent from my iPhone


>>
>> On 2.5.2009, at 0.46, dwaonsolo <nort...@hotmail.co.uk> wrote:
>>
>>
>>
>>
>>
>>>>>>> product = Product.create(:title => 'A Product', :area =>
>>>>>>> area, :sellers => [seller], :bought_at => Time.now, :type =>
>>>>>>> 'Book', :description => 'The description', :new_old =>
>>>>>>> 'old', :price => 30.5)
>>>>> ActiveRecord::StatementInvalid: Mysql::Error: Column 'area_id'
>>>>> cannot
>>>>> be null: INSERT INTO products (`price`, `created_at`, `new_old`,
>>>>> `product_image`, `title`, `updated_at`, `area_id`, `type`,
>>>>> `bought_at`, `description`) VALUES(30.5, '2009-05-01 21:05:39',
>>>>> 'old',
>>>>> NULL, 'A Product', '2009-05-01 21:05:39', NULL, NULL, '2009-05-01
>>>>> 21:05:37', 'The description')
>>
>>>> The error says it all. You're not setting the value of area_id,
>>>> which
>>>> is a NOT NULL column in your database.
>>
>>>> Add either :area_id => some_valid_area_id into the create call or
>>>> set
>>>> product.area before saving it.
>>
>>> This the code i put in.
>>
>>> product = Product.create(
>>> :title => 'A Product',
>>>

>>> :sellers => [seller],
>>> :bought_at => Time.now,
>>> :type => 'Book',
>>> :description => 'The description',
>>> :new_old => 'old',
>>> :price => 30.5
>>> )
>>
>>> where would i insert that code in?
>>
>
>

> So should i turn
>
> :area => 'area'
>
> into
>
> :area => '1'

I'm assuming your Product class looks something like this:

class Product < ActiveRecord::Base
...
belongs_to :area
...
end

and Area:

class Area < ActiveRecord::Base
...
has_many :products
...
end

I'm assuming that because you have a field called area_id in the
products table (and you're referring to area in your code).

That means that your product objects have accessors to the area object
of that particular product:

product.area # returns set area
product.area = area

product = Product.create(:title => 'A Product', :area => area)

Note that in all these cases area is an object of the Area class. Not
an integer, not a string. An Area object.

However, the way the associations between products and areas is
handled in the database layer is by using foreign keys. In
ActiveRecord, they are basically of form class_name_id (e.g. area_id).
Thus you can also set the foreign_key field directly, and the two
following code snippets are functionally equivalent:

product.area = some_area
product.save

product.area_id = some_area.id
product.save

If you use the foreign_key form above, it is by default an integer.

So, to answer your question, no. If you use the :area attribute, it
must be an Area object. If you use :area_id, it should be the primary
key of that given Area object. But in no case should it be a string.
'area' is a string, as is '1' *). Everything surrounded by quotes
(either single or double) is a string in Ruby.

//jarkko

*) Rails will automatically cast '1' as integer 1 when the field is an
integer field, but it's not relevant in this case.

Reply all
Reply to author
Forward
0 new messages