问题描述
在创建模型新政,我用一个after_create创建的DealPrize表奖。
新政和DealPrize有一个属于/的has_many关系:一锤子有很多新政的奖品和Dealprize属于新政
。它的工作原理是这样的:内幕交易,我有一列'奖号码',我用一个after_create使evetytime的amdin创建一个新的协议,应用程序借此prize_number列,创建奖品此卷(插入尽可能多的行作为必要)DealPrize表里面。
我无法用我的Rails应用MOEL变量PostgreSQL的查询中。
下面是一个工作查询(用数字代替变量)。它perfetcly在我的数据库写入吧。
模型deals.rb
CONNEXION =的ActiveRecord :: Base.connection来
高清create_prizes
Deal.transaction做
self.120000.times做|我|
CONNEXION.executeINSERT INTO deal_prizes(deal_id,created_at,的updated_at)
值(62,'2009-01-23 20点21分13秒,20点21分13秒2009-01-23')
结束
结束
结束
但是,当我更换示例值的不同而变化,因为它应该是,我得到一个错误。
CONNEXION =的ActiveRecord :: Base.connection来
高清create_prizes
Deal.transaction做
self.120000.times做|我|
CONNEXION.executeINSERT INTO deal_prizes(deal_id,created_at,的updated_at)
值(self.id,'2009-01-23 20时21分13秒,20时21分13秒2009-01-23')
结束
结束
结束
如果我用self.if,我得到错误
错误:缺少FROM子句条目表自我
我也尝试self.id,ID,deal.id,deal_id,self_id,没有任何工程。
感谢您的帮助,
编辑 - 添加了dimakura
在优化与原始的SQL和交易:
高清create_prizes
self.1200000.times做
奖品= DealPrize.create(:deal_id => self.id,:admin_user_id => self.admin_user_id)
prizes.save
结束
结束
最新code的帮助下dimakura
CONNEXION =的ActiveRecord :: Base.connection来
高清create_prizes
Deal.transaction做
self.1200000.times做|我|
ST = CONNEXION.raw_connection。prepare(INSERT INTO deal_prizes(deal_id,created_at,的updated_at)值(?,?,?))
st.execute(self.id,Time.now,Time.now)
st.close
结束
结束
结束
获取错误:
错误的参数号(1为2..3)
它应该是:
CONNEXION.executeINSERT INTO deal_prizes(deal_id,created_at,的updated_at)
值(#{self.id},'2009-01-23 20时21分13秒,20时21分13秒2009-01-23')
但它不是一个推荐的方法。你不应该插参数,是这样的。
更好的方法是:
ST = CONNEXION.raw_connection。prepare(INSERT INTO deal_prizes(deal_id,created_at,的updated_at)值(?,?,?))
st.execute(self.id,Time.now,Time.now)
st.close
When creating a model Deal, I use an after_create to create prizes on the DealPrize table.
Deal and DealPrize have a belong to/has_many relations: a Deal has many Deal prizes and a Dealprize belongs to a Deal.
It works like this: inside Deal, I have a column 'prize-number' and I use an after_create so that evetytime the amdin creates a new deal, the app takes this prize_number column, and create this volume of prizes (inserting as many rows as necessary) inside the DealPrize table.
I fail to use my Rails app moel variable inside a postgresql query.
Here is a working query (using figures instead of variables). It perfetcly writes it on my database.
model deals.rb
CONNEXION = ActiveRecord::Base.connection
def create_prizes
Deal.transaction do
self.120000.times do |i|
CONNEXION.execute "INSERT INTO deal_prizes (deal_id, created_at, updated_at)
values ( 62, '2009-01-23 20:21:13', '2009-01-23 20:21:13')"
end
end
end
But when I replace the example values by variables as it should be, I get an error.
CONNEXION = ActiveRecord::Base.connection
def create_prizes
Deal.transaction do
self.120000.times do |i|
CONNEXION.execute "INSERT INTO deal_prizes (deal_id, created_at, updated_at)
values ( self.id, '2009-01-23 20:21:13', '2009-01-23 20:21:13')"
end
end
end
If I use self.if, I get error
ERROR: missing FROM-clause entry for table "self"
I tried also self.id, id, deal.id, deal_id, self_id, nothing works.
thanks for your help,
EDIT - ADDED for dimakura
Before optimizing with raw sql and transactions:
def create_prizes
self.1200000.times do
prizes = DealPrize.create(:deal_id => self.id, :admin_user_id => self.admin_user_id)
prizes.save
end
end
LATEST CODE with the help of dimakura
CONNEXION = ActiveRecord::Base.connection
def create_prizes
Deal.transaction do
self.1200000.times do |i|
st = CONNEXION.raw_connection.prepare("INSERT INTO deal_prizes (deal_id, created_at, updated_at) values (?, ?, ?)")
st.execute(self.id, Time.now, Time.now)
st.close
end
end
end
Getting error:
wrong number of arguments (1 for 2..3)
It should be:
CONNEXION.execute "INSERT INTO deal_prizes (deal_id, created_at, updated_at)
values ( #{self.id}, '2009-01-23 20:21:13', '2009-01-23 20:21:13')"
But it's not a recommended approach. You should never interpolate parameters like this.
The better approach is:
st = CONNEXION.raw_connection.prepare("INSERT INTO deal_prizes (deal_id, created_at, updated_at) values (?, ?, ?)")
st.execute(self.id, Time.now, Time.now)
st.close
这篇关于错误:缺少FROM子句条目表"自" (轨道4活动记录的事务时,PostgreSQL 9.4))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!