Pages

Thursday 26 January 2012

(ActiveRecord::RecordNotUnique) "PGError: ERROR: duplicate key value violates unique constraint

Today when pushed the code on production a strange notification get starts mailing that record is not unique ... the primary key is being getting duplicate which is supposed to be unique and was set to auto-increment.

Pressure keep rising as the site was live and the delete functionality was getting crash here investigation started as.

  1. Created a record from console and it get saved perfectly and get the id 225.
  2. Check the last record of the culprit table and the id was 983
  3. Here it clicks that wrapper of active model was introduced later and the table was being getting dumped through sql.
  4. Take little help from google to check how postgres handles primary key and it comes to know that postgres maintains Sequence structure for primary keys and just keep single filed ++ for the new records unique id. 
  5. So simply update the relevant tables primary key fields sequencer to the max id of the table and it starts getting smooth and no more Record Not Unique exception raised.
And this is how a strange issue of primary get resolved and came to know that the Rocket Science of DB's primary keys.

Cheer :)



HTTP Status Code usage

Status code specially using in Ajax calls.

Using http status code is now getting in practice and should be used to take advantages of these codes instead of handling them manually.

Making it simple by categorizing the response either success or failure where there responding codes are 200 and 400.

In context of rails there can be two ways to handle the response.

  • First one the bad one

render :text => "Its working" and return

Now on rendering page we need to put string comparison to find out that either its success or failure response.

  • Second one the good one
render :text => "Intelligent Text", :status => 200 and return 

Now here we don't need to make any comparison of responded text the status code will automatically map to relevant response.

like 

ajax:success( // do success stuff)
ajax:failure(  // do failure stuff)

That's it the relevant function will be invoked on basis of relevant status code.

Good to use status code.

:)

Monday 16 January 2012

Rails 3.1.3 Assets Bug

A week ago working with a project and got a feel that my forms are being getting post more than one with random behavior, firstly i got a doubt on code bug but all was normal and then while debugging css i just saw in console that whole css has been loaded twice so this gives me a hint to the problem …

then here i start taking help of Sir Google and in one of blog i found a link that application.css or application.js use to load all files in assets folder and its getting load both compiled and non-compiled assets.

So the simple solution was to define assets for development environment by config.assets.prefix = “/devassets”

and this gives me a really relief as i have spent a lot of time in wondering the reason of forms getting multiple time posts even i tried to down grade rails to get rid of this problem but caught in more problems.

Resque Web Console

Resque a perfect queuing system up till now and well documented but i was unable to get that how to configure it with in application instead of running it separately …

As Resque console is sanitara app so we can mount it in rails 3 by just adding it in routes file

mount Resque::Server, :at=> “/resque”

and to add http authentication just add configuration file in initializers with following code

Resque::Server.use(Rack::Auth::Basic) do |user, password|

password == “password”

end

That’s it you are good to go with resque web console with your application server.

:)

Pusher Push Notification to Browser

Today while me and my friend was using facebook sitting side by side and he like our mutual friends status and with in 2 seconds that like information pop up in my recent friend activities … got amazed to see that and here start exploring the Rocket Science of this feature ….

Open my console and try to see that is our browser side is polling and found nothing then this ticked in mind that this is some thing like UAShip calls to smart phones (iOS, Android) so just here Mr Google help me to find service to send notification to web like device and this gives me puhser.com that is using Websocket science to implement this feature.

Well documented to test a single alert box message push to browser and really cool to have it different projects for server to update client instead of continuous polling from client side.

http://en.wikipedia.org/wiki/WebSocket
http://pusher.com/