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 -- Posted via http://www.ruby-forum.com/.
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 First <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.
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 First <rails-mailing-l...-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt>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 -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
On Sun, Nov 1, 2009 at 11:43 PM, Vishnu First < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 First <rails-mailing-l...@andreas-s.net> > > 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 >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> -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 .. > > > > Sijohi 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 -- Posted via http://www.ruby-forum.com/.