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
>
>
>
> 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.