views:

13

answers:

1

I am trying to use psql triggers in my rails app. So i tried using this migration where execution of psql triggers are supposedly easly -- class AddTriggersToProducts < ActiveRecord::Migration def self.up table :products execute %q{ create trigger trig1 before insert on products for each row begin price = price + 5 end;

    }

end

def self.down execute 'DROP TRIGGER trig1' end end But this didnt change anything... I dont know where to write the procedure or function if i am gonna use one here...

A: 

Does something like this work? Creating a function and then executing the function for the trigger:

   def self.up 
     execute %q{
       create or replace function update_price() returns trigger as $$
         begin
           NEW.price := NEW.price + 5;
           return NEW;
         end;
       $$ language plpgsql }

      execute %{ create trigger trig1 before insert on products for each row execute function update_price()}
  end
njorden