Relating Namespaced Models


#1

I am dealing with a large project. I use a lot of STI and therfore have
a lot of models. I’d like to use namespaces to help organize my models
but am having trouble getting started.

I have a base model Term::Base located in /app/models/term/base.rb:

class Term::Base < ActiveRecord::Base
set_table_name “terms”
end

I have a non-namespaced client model:

class Client < ActiveRecord::Base
has_many :terms, :class_name => “Term::Base”
end

When I try to access a client’s terms, i get:

Client.find(1).terms
ArgumentError:
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_support/dependencies.rb:399:in
to_constant_name': Anonymous modules have no name to be referenced by from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:1363:incompute_type’
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_support/dependencies.rb:211:in
qualified_name_for' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_support/dependencies.rb:470:inconst_missing’
from (eval):1:in compute_type' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:1066:ininstantiate_without_callbacks’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/callbacks.rb:204:in
instantiate' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:424:infind_by_sql’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:424:in
collect!' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:424:infind_by_sql’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:994:in
find_every' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:415:infind’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/associations/has_many_association.rb:91:in
find' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/associations/association_collection.rb:159:infind_target’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/associations/has_many_association.rb:123:in
load_target' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/associations/association_proxy.rb:122:inmethod_missing’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/associations/has_many_association.rb:98:in
method_missing' from /usr/local/lib/ruby/1.8/irb.rb:298:inoutput_value’
from /usr/local/lib/ruby/1.8/irb.rb:151:in eval_input' from /usr/local/lib/ruby/1.8/irb.rb:259:insignal_status’
from /usr/local/lib/ruby/1.8/irb.rb:147:in eval_input' from /usr/local/lib/ruby/1.8/irb.rb:146:ineval_input’
from /usr/local/lib/ruby/1.8/irb.rb:70:in start' from /usr/local/lib/ruby/1.8/irb.rb:69:incatch’
from /usr/local/lib/ruby/1.8/irb.rb:69:in `start’
from /usr/local/bin/irb:13>>

It works if I “unnamespace” my Term::* models by putting them in
/app/models/ and removing the Term:: prefixes and of course removing
the :class_name => “Term::Base” from the association.

Any ideas fellas?

Todd Boland


#2

try using
:class_name => “Term/Base”

–jake

itodd wrote:

I am dealing with a large project. I use a lot of STI and therfore have
a lot of models. I’d like to use namespaces to help organize my models
but am having trouble getting started.

I have a base model Term::Base located in /app/models/term/base.rb:

class Term::Base < ActiveRecord::Base
set_table_name “terms”
end

I have a non-namespaced client model:

class Client < ActiveRecord::Base
has_many :terms, :class_name => “Term::Base”
end

When I try to access a client’s terms, i get:

Client.find(1).terms
ArgumentError:
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_support/dependencies.rb:399:in
to_constant_name': Anonymous modules have no name to be referenced by from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:1363:incompute_type’
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/lib/active_support/dependencies.rb:211:in
m /usr/local/lib/ruby/1.8/irb.rb:69:in `start’
from /usr/local/bin/irb:13>>

It works if I “unnamespace” my Term::* models by putting them in
/app/models/ and removing the Term:: prefixes and of course removing
the :class_name => “Term::Base” from the association.

Any ideas fellas?

Todd Boland