aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-05-29 03:48:17 +0000
committerRick Olson <technoweenie@gmail.com>2006-05-29 03:48:17 +0000
commit06075a9eb5ddf255ed561bc4cdddef214d7c63aa (patch)
tree229caa32d0b9815e8f58d8b39d6cee1cfa1a1d00 /activerecord/lib
parent8498c74a99d4db42060dcf4be1d8270c755a5bdc (diff)
downloadrails-06075a9eb5ddf255ed561bc4cdddef214d7c63aa.tar.gz
rails-06075a9eb5ddf255ed561bc4cdddef214d7c63aa.tar.bz2
rails-06075a9eb5ddf255ed561bc4cdddef214d7c63aa.zip
Fix the has_and_belongs_to_many #create doesn't populate the join for new records. Closes #3692 [josh@hasmanythrough.com]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4379 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb6
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb11
2 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index e1a0289039..ff10763cbf 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -763,6 +763,10 @@ module ActiveRecord
# * <tt>collection.size</tt> - returns the number of associated objects.
# * <tt>collection.find(id)</tt> - finds an associated object responding to the +id+ and that
# meets the condition that it has to be associated with this object.
+ # * <tt>collection.build(attributes = {})</tt> - returns a new object of the collection type that has been instantiated
+ # with +attributes+ and linked to this object through the join table but has not yet been saved.
+ # * <tt>collection.create(attributes = {})</tt> - returns a new object of the collection type that has been instantiated
+ # with +attributes+ and linked to this object through the join table and that has already been saved (if it passed the validation).
#
# Example: An Developer class declares <tt>has_and_belongs_to_many :projects</tt>, which will add:
# * <tt>Developer#projects</tt>
@@ -775,6 +779,8 @@ module ActiveRecord
# * <tt>Developer#projects.empty?</tt>
# * <tt>Developer#projects.size</tt>
# * <tt>Developer#projects.find(id)</tt>
+ # * <tt>Developer#projects.build</tt> (similar to <tt>Project.new("project_id" => id)</tt>)
+ # * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new("project_id" => id); c.save; c</tt>)
# The declaration may include an options hash to specialize the behavior of the association.
#
# Options are:
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
index cc6549f3ae..42cf549dcb 100644
--- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -13,6 +13,17 @@ module ActiveRecord
record
end
+ def create(attributes = {})
+ # Can't use Base.create since the foreign key may be a protected attribute.
+ if attributes.is_a?(Array)
+ attributes.collect { |attr| create(attr) }
+ else
+ record = build(attributes)
+ insert_record(record) unless @owner.new_record?
+ record
+ end
+ end
+
def find_first
load_target.first
end