hi,
I am doing unit testing. I have a table as SalesOrder, where
“ordernumber” is the primary key of the table. When I insert records in
the table from unit testing, I got error like,
ActiveRecord::RecordNotFound: Couldn’t find SalesOrder without an ID
But, I have fixed the error by adding set_primary_key “ordernumber” in
the model, and tested the unit test class and works well. But, If I
insert record from the application as SalesOrder.save, I got the same
error. If I comment the line set_primary_key in the model, then the
application works fine, but unit testing fails with this error.
Is there any alternative way, where unit test and application accepts?
Thank you
Vishnu,
What’s wrong with using “ID” as the primary key instead of
“ordernumber”? I would strongly advise doing so; you will most likely
save yourself (and others) current and future headaches by adopting
this practice.
Best regards,
Sebastian
On Oct 31, 4:15 pm, Vishnu F. [email protected]
Sebastian von Conrad wrote:
Vishnu,
What’s wrong with using “ID” as the primary key instead of
“ordernumber”? I would strongly advise doing so; you will most likely
save yourself (and others) current and future headaches by adopting
this practice.
Best regards,
Sebastian
On Oct 31, 4:15�pm, Vishnu F. [email protected]
Sebastian,
Thank you for your reply. Its right to keep “ID” as primary key. But, my
client requirement is to keep the “ordernumber” as primary key. So, I am
trying to fix with this.
Thank you
Hi Vishnu
In the normal case the procedure is like
create_table :sales_orders, :id => false do |t|
t.integer :ordernumber, :primary => true
end
Now in the class SalesOrder you can define like
set_primary_key “ordernumber”
And also you have to write a before_create call back to set the
ordernumber by some means to make it uniq
self.ordernumber = getuniqnumber
Is what u did so far like above?
If it is a legacy db you can set like
set_table_name , set_primary_key, set_sequence_name …
Sijo
Sijo k g wrote:
Hi Vishnu
In the normal case the procedure is like
create_table :sales_orders, :id => false do |t|
t.integer :ordernumber, :primary => true
end
Now in the class SalesOrder you can define like
set_primary_key “ordernumber”
And also you have to write a before_create call back to set the
ordernumber by some means to make it uniq
self.ordernumber = getuniqnumber
Is what u did so far like above?
If it is a legacy db you can set like
set_table_name , set_primary_key, set_sequence_name …
Sijo
hi Sijo,
I have did the same as you said as set_primary_key “ordernumber†in the
model class as before. I am creating model class object from the unit
test class, by getting values from the fixtures. The object creation is
fine, when I mention the statement set_primary_key ‘ordernumber’ in the
model class and running the unit test class with no errors. But, When I
run the rails application normally (not from unit test class), it
doesn’t create object, it throws exception, when the set_primary_key is
present. If I comment the line, then, the application runs without
error. Error as
ActiveRecord::RecordNotFound: Couldn’t find SalesOrder without an ID
Thank you
On Sun, Nov 1, 2009 at 11:43 PM, Vishnu F. <
[email protected]> wrote:
Sebastian
The id field is really used for database record management and it tends
to
be a read-only field after the model instance is created. What happens
when
the format of the order number changes? You might want to read and
fully
understand the consequences by reading section 18.3 of AWDwR 3ed.
Good luck,
-Conrad