【发布时间】:2011-06-01 05:10:16
【问题描述】:
我有一个模型如下:
class EntityTag < ActiveRecord::Base
attr_protected :user_id, :post_id, :entity_id
belongs_to :user
belongs_to :post
belongs_to :entity
validates :user_id, :presence => true
validates :entity_id, :presence => true
validates :post_id, :presence => true
end
我想防止多行具有相同的 user_id、entity_id 和 post_id 组合(例如,一行的唯一 ID 是所有这三个值)。
与 ActiveRecord 交流的最简单方法是什么?
【问题讨论】:
-
validates_uniqueness_of :user_id, :scope => [:entity_id, :post_id]
标签: ruby-on-rails activerecord model