aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-07-04 14:51:12 -0600
committerSean Griffin <sean@thoughtbot.com>2014-07-04 16:10:45 -0600
commit00f55516509770f58f9ce462d45afb80d8f649ca (patch)
tree800d823a63deec4d35e229cf43f547c038d9202a /activerecord/lib/active_record/associations.rb
parenta6cc7b0ebd608a5100df17b4cee182b67643baac (diff)
downloadrails-00f55516509770f58f9ce462d45afb80d8f649ca.tar.gz
rails-00f55516509770f58f9ce462d45afb80d8f649ca.tar.bz2
rails-00f55516509770f58f9ce462d45afb80d8f649ca.zip
Add a `required` option to singular associations
In addition to defining the association, a `required` association will also have its presence validated. Before: ```ruby belongs_to :account validates_presence_of :account ``` After: ```ruby belongs_to :account, required: true ``` This helps to draw a distinction between types of validations, since validations on associations are generally for data integrity purposes, and aren't usually set through form inputs.
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rw-r--r--activerecord/lib/active_record/associations.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 6222bfe903..d9b339a1f6 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1309,6 +1309,10 @@ module ActiveRecord
# that is the inverse of this <tt>has_one</tt> association. Does not work in combination
# with <tt>:through</tt> or <tt>:as</tt> options.
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
+ # [:required]
+ # When set to +true+, the association will also have its presence validated.
+ # This will validate the association itself, not the id. You can use
+ # +:inverse_of+ to avoid an extra query during validation.
#
# Option examples:
# has_one :credit_card, dependent: :destroy # destroys the associated credit card
@@ -1320,6 +1324,7 @@ module ActiveRecord
# has_one :boss, readonly: :true
# has_one :club, through: :membership
# has_one :primary_address, -> { where primary: true }, through: :addressables, source: :addressable
+ # has_one :credit_card, required: true
def has_one(name, scope = nil, options = {})
reflection = Builder::HasOne.build(self, name, scope, options)
Reflection.add_reflection self, name, reflection
@@ -1421,6 +1426,10 @@ module ActiveRecord
# object that is the inverse of this <tt>belongs_to</tt> association. Does not work in
# combination with the <tt>:polymorphic</tt> options.
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
+ # [:required]
+ # When set to +true+, the association will also have its presence validated.
+ # This will validate the association itself, not the id. You can use
+ # +:inverse_of+ to avoid an extra query during validation.
#
# Option examples:
# belongs_to :firm, foreign_key: "client_of"
@@ -1433,6 +1442,7 @@ module ActiveRecord
# belongs_to :post, counter_cache: true
# belongs_to :company, touch: true
# belongs_to :company, touch: :employees_last_updated_at
+ # belongs_to :company, required: true
def belongs_to(name, scope = nil, options = {})
reflection = Builder::BelongsTo.build(self, name, scope, options)
Reflection.add_reflection self, name, reflection