Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Validates Foreign Key

Validates Foreign Key

This validation module calls validates_presence_of with an :if argument to allow verifying the existence of either the foreign key or an actual instance of an associated object. For example:

class Foo < ActiveRecord::Base has_many :bars end class Bar < ActiveRecord::Base belongs_to :foo validates_presence_of_foreign_key :foo validates_associated :foo end

The above is equivalent to just adding the following to Bar:

validates_presence_of :foo_id, :if => Proc.new { |o| o.foo.blank? }

You can call validates_presence_of_foreign_key with one or more column names. The column name can either by a symbol or an Array containing two symbols. In the former event, the name of the foreign key is extrapolated from the name of the object, e.g., :foo becomes :foo_id. In the latter case the name of the associated object is assumed to be the first element and the name of the foreign key is assumed to be the second element.

validates_presence_of :foo, :bar, [:baz, :some_weird_id], :quux

This module also accepts the :message and :on options which are simply passed to validates_presence_of.

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all 2 member details


Membership

+ Join this railsplugin

Record Maintainer

'None'