根据datamapper文档,正确的方法是
MERGE INTO
Photo p
USING
TmpPhoto tp ON p.ProductNumberID = tp.ProductNumberID
AND p.SHA1 = tp.SHA1
WHEN
MATCHED AND p.Rank = tp.Rank
THEN
UPDATE
SET p.VerifiedDate = GETDATE(),
p.Rank = CASE
WHEN p.Rank != tp.Rank AND tp.Rank != 1 THEN tp.Rank
WHEN p.Rank != tp.Rank AND tp.Rank = 1 THEN tp.Rank
ELSE p.Rank END,
p.Active = CASE
WHEN p.Rank != tp.Rank AND tp.Rank != 1 THEN 0
WHEN p.Rank != tp.Rank AND tp.Rank = 1 THEN 1
ELSE p.Active END
WHEN
NOT MATCHED
THEN
INSERT (ProductNumberID, Code, Extension, Rank, CreatedDate, VerifiedDate, FCTH, SHA1, Active)
VALUES (tp.ProductNumberID, tp.Code, tp.Extension, tp.Rank, getdate(), getdate(), tp.FCTH, tp.SHA1, 0)
OUTPUT inserted.NumberID as PhotoNumberID, inserted.ProductNumberID, inserted.SHA1, inserted.Rank INTO InsertedPhotos;
假设thing = Thing.first_or_create({
conditions: ['lower(value) = ?', some_value.downcase]
}, {
value: some_value.downcase
})
正确返回第一条记录,Thing.first(conditions: ['lower(value) = ?', some_value.downcase])
如果查询与初始值不同,则接受两个参数。
然而,上面抛出
first_or_create
我目前使用的代码只是单独执行The attribute 'conditions' is not accessible in Thing
和first
。
create
但考虑到thing = Thing.first(conditions: ['lower(value) = ?', some_value.downcase])
thing = Thing.create(value: some_value.downcase) if email.nil?
的存在,我认为必须有更正确的方法。