Как сделать has_many с моделью, которая находится в пространстве имен - PullRequest
11 голосов
/ 10 декабря 2010

Rails 3 дал нам эти замечательные генераторы, которые помещают модели и контроллеры в пространства имен.Но теперь я не могу связать их с другими моделями.

Я пытаюсь достичь has_many через ассоциацию с моделями пространства имен.Я искал в интернете несколько дней и не нашел ни одного примера.

/ 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

А вот и ошибка

/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

1 Ответ

12 голосов
/ 10 декабря 2010

Я сам не пытался использовать пространства имен для моделей, но при указании класса для ассоциации вы должны использовать: class_name вместо: class. Остальное выглядит хорошо для меня, так что это может быть единственное, что вам нужно изменить.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...