1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

support anonymous classes on has_many associations

This commit is contained in:
Aaron Patterson 2013-09-03 14:16:29 -07:00
parent c64a001dcc
commit 1f006cd5f1
3 changed files with 20 additions and 1 deletions

View file

@ -17,7 +17,7 @@ module ActiveRecord::Associations::Builder
end
self.extensions = []
VALID_OPTIONS = [:class_name, :foreign_key, :validate]
VALID_OPTIONS = [:class_name, :class, :foreign_key, :validate]
attr_reader :name, :scope, :options

View file

@ -121,6 +121,7 @@ module ActiveRecord
@scope = scope
@options = options
@active_record = active_record
@klass = options[:class]
@plural_name = active_record.pluralize_table_names ?
name.to_s.pluralize : name.to_s
end

View file

@ -45,6 +45,24 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
Client.destroyed_client_ids.clear
end
def test_anonymous_has_many
developer = Class.new(ActiveRecord::Base) {
self.table_name = 'developers'
dev = self
developer_project = Class.new(ActiveRecord::Base) {
self.table_name = 'developers_projects'
belongs_to :developer, :class => dev
}
has_many :developer_projects, :class => developer_project, :foreign_key => 'developer_id'
}
dev = developer.first
named = Developer.find(dev.id)
assert_operator dev.developer_projects.count, :>, 0
assert_equal named.projects.map(&:id).sort,
dev.developer_projects.map(&:project_id).sort
end
def test_create_from_association_should_respect_default_scope
car = Car.create(:name => 'honda')
assert_equal 'honda', car.name