English 中文(简体)
铁路3 独一无二的特性鉴定,范围包括多形态表
原标题:Rails 3 uniqueness validation with scope on polymorphic table

下面是:

class Vote < ActiveRecord::Base
  belongs_to :voteable, :polymorphic => :true, :counter_cache => true
end

class Proposition < ActiveRecord::Base
  has_many :votes, :as => :voteable
end

class Winner < ActiveRecord::Base
  has_many :votes, :as => :voteable
end

投票表认为:

t.string   "ip_address"
t.integer  "voteable_id"
t.string   "voteable_type"

我要证实以下几点。 拥有一定比例的用户只能就1项主张进行表决。 因此,Pip_address、可投票_id和可投票_等组合需要独一无二。

如何用“简单”验证规则做到这一点?

最佳回答

为保证您的独特性,您必须增加其非行的独特指数。

If you don t have important data yet you can do it inside migration with add_index

add_index(:votes, [:ip_address, :voteable_id, voteable_type], :unique => true, :name =>  allowed_one_vote )

如果你已经掌握了一些数据,则可以做。

问题回答

增加独一无二的<代码>的范围:ip_address 审定

class Vote < ActiveRecord::Base
  # ...
  validates :ip_address, uniqueness: { scope: [:voteable_type, :voteable_id]}
end




相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签