Showing posts with label ruby on rails. Show all posts
Showing posts with label ruby on rails. Show all posts

Friday, 8 February 2008

RoR: script/console autocompletetion

script/console is a very useful tool when building your RoR application.
And one of my favorite features is the ability to autocomplete the method name (just by pressing tab).
This gives me a kind of Ruby on Rails IDE taste, since when I'm at Windows I (happily) use Notepad++ (which has a ruby plugin) to code.

Thursday, 31 January 2008

ActiveRecord error : duplicate key violates unique constraint

I'm currently working (and still exploring) Ruby on Rails for a VoIP services management web portal project.
I frequently stumble upon some doubts about the correct RoR way of doing things - I admit, I tend to do it java style =( - but this one was really driving me crazy...
When creating a new record for a table named "groups", the ActiveRecord was hitting me with the following exception:

«ActiveRecord::StatementInvalid: RuntimeError: ERROR C23505
Mduplicate key violates unique constraint "pk_groups"»

after some "googlening", I manage to solve this. Apparently, the problem was with the expected sequence, from RoR, for that table "groups".
Since the postgresql was returning the correct next sequence number:

# select nextval('groups_group_id_seq');
nextval
---------
17
(1 row)

it was only a matter of resetting the ActiveRecord sequence (on script/console)
>> ActiveRecord::Base.connection.reset_pk_sequence!('groups')
=> "17"