如何在rails中创建postgres触发器?

时间:2022-09-16 09:07:12

I have a table product(id,name) and table product_raiting(product_id,rate_type,rate). When I create new product, I need to create three raitings for this product.

我有一个表产品(id,名称)和表product_raiting(product_id,rate_type,rate)。当我创建新产品时,我需要为此产品创建三个raitings。

How can I add postgres trigger with do something like:

如何添加postgres触发器,例如:



    product.create
    product.product_raitings.create( :rate_type => 0, :rate => 0 )
    product.product_raitings.create( :rate_type => 1, :rate => 0 )
    product.product_raitings.create( :rate_type => 2, :rate => 0 )
    product.product_raitings.create( :rate_type => 3, :rate => 0 )

Please help.

1 个解决方案

#1


8  

Do you want to create a trigger in the Database so on insert the DB does the work?

你想在数据库中创建一个触发器,这样插入数据库就可以了吗?

http://www.postgresql.org/docs/9.1/static/sql-createtrigger.html

CREATE FUNCTION addRaitings() RETURNS TRIGGER AS '
   BEGIN
      INSERT INTO product_raitings(product_id,rate_type,rate) values(NEW.id,0,0);
      INSERT INTO product_raitings(product_id,rate_type,rate) values(NEW.id,1,0);
      INSERT INTO product_raitings(product_id,rate_type,rate) values(NEW.id,2,0);
      INSERT INTO product_raitings(product_id,rate_type,rate) values(NEW.id,3,0);
   END;
   ' LANGUAGE plpgsql;

CREATE TRIGGER createRaitingTrigger
        AFTER INSERT ON products
        FOR EACH ROW
        EXECUTE PROCEDURE addRaitings();

or do you want rails to do the work? then it would be an after_create callback http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

或者你想让rails做这项工作?然后它将是一个after_create回调http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

so perhaps something like this.

所以也许是这样的。

class Product < ActiveRecord::Base
  has_many product_raitings

  after_create :build_default_raitings

 private
 def build_default_raitings
    (0..3).each { |x| self.product_raitings.create(:rate_type => x, :rate => 0) }
 end
end

#1


8  

Do you want to create a trigger in the Database so on insert the DB does the work?

你想在数据库中创建一个触发器,这样插入数据库就可以了吗?

http://www.postgresql.org/docs/9.1/static/sql-createtrigger.html

CREATE FUNCTION addRaitings() RETURNS TRIGGER AS '
   BEGIN
      INSERT INTO product_raitings(product_id,rate_type,rate) values(NEW.id,0,0);
      INSERT INTO product_raitings(product_id,rate_type,rate) values(NEW.id,1,0);
      INSERT INTO product_raitings(product_id,rate_type,rate) values(NEW.id,2,0);
      INSERT INTO product_raitings(product_id,rate_type,rate) values(NEW.id,3,0);
   END;
   ' LANGUAGE plpgsql;

CREATE TRIGGER createRaitingTrigger
        AFTER INSERT ON products
        FOR EACH ROW
        EXECUTE PROCEDURE addRaitings();

or do you want rails to do the work? then it would be an after_create callback http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

或者你想让rails做这项工作?然后它将是一个after_create回调http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

so perhaps something like this.

所以也许是这样的。

class Product < ActiveRecord::Base
  has_many product_raitings

  after_create :build_default_raitings

 private
 def build_default_raitings
    (0..3).each { |x| self.product_raitings.create(:rate_type => x, :rate => 0) }
 end
end