Rails 3 gave us these nice generators that place models and controllers into namespaces, whoo! But now I'm unable to associate them with other models.
I'm trying to achieve a has_many through association with namespaced models. I've been searching around the internet for a few days and I haven't come up with any examples.
/app/models/templates/practice.rb
class Templates::Practice < ActiveRecord::Base
has_many :practice_sequences, :order => "position", :dependent => :destroy, :class => 'Templates::PracticeSequence'
has_many :seasons, :through => :practice_sequences, :order => "position", :class => 'Templates::Season'
end
/app/models/templates/season.rb
class Templates::Season < ActiveRecord::Base
has_many :practice_sequences, :order => "position", :class => 'Templates::PracticeSequence'
has_many :practices, :through => :practice_sequences, :order => "position", :class => 'Templates::Practice'
end
/app/models/templates/practice_season.rb
class Templates::PracticeSequence < ActiveRecord::Base
belongs_to :practice, :class => 'Templates::Practice'
belongs_to :season, :class => 'Templates::Season'
end
And here is the error
/Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.1/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys': Unknown key(s): class (ArgumentError)
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activerecord-3.0.1/lib/active_record/associations.rb:1739:in `create_has_many_reflection'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activerecord-3.0.1/lib/active_record/associations.rb:983:in `has_many'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activerecord-3.0.1/lib/active_record/autosave_association.rb:137:in `has_many'
from /Users/jspooner/Documents/SpoonerMedia/FooBars/coach_app/todo/app/models/templates/practice.rb:3
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:227:in `load_dependency'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:346:in `require_or_load'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:300:in `depend_on'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:216:in `require_dependency'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/engine.rb:138:in `eager_load!'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/engine.rb:137:in `each'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/engine.rb:137:in `eager_load!'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/engine.rb:135:in `each'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/engine.rb:135:in `eager_load!'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/application.rb:108:in `eager_load!'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/application/finisher.rb:41
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `instance_exec'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `run'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/initializable.rb:50:in `run_initializers'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `each'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `run_initializers'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/application.rb:134:in `initialize!'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/application.rb:77:in `send'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/railties-3.0.1/lib/rails/application.rb:77:in `method_missing'
from /Users/jspooner/Documents/SpoonerMedia/FooBars/coach_app/todo/spec/../config/environment.rb:5
from /Users/jspooner/Documents/SpoonerMedia/FooBars/coach_app/todo/spec/spec_helper.rb:12:in `require'
from /Users/jspooner/Documents/SpoonerMedia/FooBars/coach_app/todo/spec/spec_helper.rb:12
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/spork-0.9.0.rc2/lib/spork.rb:23:in `prefork'
from /Users/jspooner/Documents/SpoonerMedia/FooBars/coach_app/todo/spec/spec_helper.rb:7
from /Users/jspooner/Documents/SpoonerMedia/FooBars/coach_app/todo/spec/helpers/templates/practice_templates_helper_spec.rb:1:in `require'
from /Users/jspooner/Documents/SpoonerMedia/FooBars/coach_app/todo/spec/helpers/templates/practice_templates_helper_spec.rb:1
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/rspec-core-2.2.1/lib/rspec/core/configuration.rb:327:in `load'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/rspec-core-2.2.1/lib/rspec/core/configuration.rb:327:in `load_spec_files'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/rspec-core-2.2.1/lib/rspec/core/configuration.rb:327:in `map'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/rspec-core-2.2.1/lib/rspec/core/configuration.rb:327:in `load_spec_files'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/rspec-core-2.2.1/lib/rspec/core/command_line.rb:18:in `run'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/rspec-core-2.2.1/lib/rspec/core/runner.rb:55:in `run_in_process'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/rspec-core-2.2.1/lib/rspec/core/runner.rb:46:in `run'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/rspec-core-2.2.1/lib/rspec/core/runner.rb:10:in `autorun'
from /Users/jspooner/.rvm/gems/ree-1.8.7-2010.02/gems/rspec-core-2.2.1/bin/rspec:4
I have not tried using namespaces for models myself, but when specifying a class for an association, you should use :class_name instead of :class. The rest looks good to me so that might be the only thing you need to change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With