Forum: Ruby on Rails Relating Namespaced Models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ca6bffe0507382dfacb6fdd27feea77b?d=identicon&s=25 itodd (Guest)
on 2006-12-22 21:24
(Received via mailing list)
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:in
`compute_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:in
`const_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:in
`instantiate_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:in
`find_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:in
`find_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:in
`find'
        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:in
`find_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:in
`method_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:in `output_value'
        from /usr/local/lib/ruby/1.8/irb.rb:151:in `eval_input'
        from /usr/local/lib/ruby/1.8/irb.rb:259:in `signal_status'
        from /usr/local/lib/ruby/1.8/irb.rb:147:in `eval_input'
        from /usr/local/lib/ruby/1.8/irb.rb:146:in `eval_input'
        from /usr/local/lib/ruby/1.8/irb.rb:70:in `start'
        from /usr/local/lib/ruby/1.8/irb.rb:69:in `catch'
        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
5a8ab1ea865141adaa35ab9408435003?d=identicon&s=25 Jake Varghese (jake3030)
on 2006-12-22 21:36
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:in
> `compute_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
This topic is locked and can not be replied to.