Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
has_and_belongs_to_many association support
- Loading branch information
Luca Guidi
committed
Nov 12, 2008
1 parent
4369941
commit d6281f4
Showing
13 changed files
with
380 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
lib/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module ActiveRecord | ||
module Associations | ||
class HasAndBelongsToManyAssociation < AssociationCollection #:nodoc: | ||
def insert_record(record, force=true) #:nodoc: | ||
if record.new_record? | ||
if force | ||
record.save! | ||
else | ||
return false unless record.save | ||
end | ||
end | ||
|
||
if @reflection.options[:insert_sql] | ||
@owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record)) | ||
else | ||
columns = @owner.connection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns") | ||
|
||
attributes = columns.inject({}) do |attrs, column| | ||
case column.name.to_s | ||
when @reflection.primary_key_name.to_s | ||
attrs[column.name] = owner_quoted_id | ||
when @reflection.association_foreign_key.to_s | ||
attrs[column.name] = record.quoted_id | ||
else | ||
if record.has_attribute?(column.name) | ||
value = @owner.send(:quote_value, record[column.name], column) | ||
attrs[column.name] = value unless value.nil? | ||
end | ||
end | ||
attrs | ||
end | ||
|
||
sql = | ||
"INSERT INTO #{@owner.connection.quote_table_name @reflection.options[:join_table]} (#{@owner.send(:quoted_column_names, attributes).join(', ')}) " + | ||
"VALUES (#{attributes.values.join(', ')})" | ||
|
||
@owner.connection.insert(sql) | ||
end | ||
|
||
@owner.send(:cache_delete, @reflection) if @reflection.options[:cached] | ||
|
||
return true | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.