Using Sequences to Create or Update in Rails

I created a transfer sequence that I want to use for both creating and updating. I think I can create a filter before saving, which will adjust the sequence, but I'm not sure how to "enter" the required SQL (in this case nextval('sequence')). My query should look something like this:

UPDATE users
SET 
sequence=nextval('users_sequence'),
name='Kevin',
...
WHERE id=...

Please note that I, unfortunately, cannot create a trigger for this, since I am deploying on a shared Heroku database and do not have access to PLPGSQL.

+3
source share
1 answer

In yours before_filterwrite something like:

User.connection.select_value("SELECT nextval('users_sequence')")

Hope this helps.

+1
source

All Articles