【发布时间】:2017-08-28 15:08:47
【问题描述】:
我使用的是 Rails 5.0.4(和 PostGres 9.5)。在我的模型中,我有一个跨三列的唯一约束
cindex=# \d user_notifications;
Table "public.user_notifications"
Column | Type | Modifiers
--------------------+---------+-----------------------------------------------------------------
id | integer | not null default nextval('user_notifications_id_seq'::regclass)
user_id | integer |
crypto_currency_id | integer |
price | integer | not null
buy | boolean | not null
Indexes:
"user_notifications_pkey" PRIMARY KEY, btree (id)
"uk_user_notifications" UNIQUE, btree (user_id, crypto_currency_id, buy)
"index_user_notifications_on_crypto_currency_id" btree (crypto_currency_id)
"index_user_notifications_on_user_id" btree (user_id)
我如何在我的模型中指出这个唯一约束的存在,以便我去保存我的模型时
if @user_notification.save
...
else
format.html { render action: "index" }
puts "full messages: #{@user_notification.errors.full_messages}"
format.json { render json: @user_notification.errors, status: :unprocessable_entity }
format.js { render json: { errors: @user_notification.errors, success: false }, content_type: 'application/json' }
end
保存了正确的错误,表明违反了唯一约束?
【问题讨论】:
-
三列分别是
buy、user_id和crypto_currency_id? -
是的,没错。
标签: ruby-on-rails model save ruby-on-rails-5 unique-constraint