Forum: Ruby on Rails ActiveRecord in modules & associations

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.
Garz (Guest)
on 2006-03-02 21:22
I'd like to organise my active records into modules but can't get the
associations to work. The doc says that associations within the same
module should work. I've tried using the class_name option too but still
doesn't work.
Can anyone help?

For example
module Mod1
  class Animal < ActiveRecord::Base
     has_many :dogs, :class_name => "Mod1::Dog"
  end
  class Dog < ActiveRecord::Base
     belongs_to :animal, :class_name => "Mod1::Animal"
  end
end
Garz (Guest)
on 2006-03-02 21:47
When I call animal.dogs I get "uninitialized constant Dog"

Garz wrote:
> I'd like to organise my active records into modules but can't get the
> associations to work. The doc says that associations within the same
> module should work. I've tried using the class_name option too but still
> doesn't work.
> Can anyone help?
>
> For example
> module Mod1
>   class Animal < ActiveRecord::Base
>      has_many :dogs, :class_name => "Mod1::Dog"
>   end
>   class Dog < ActiveRecord::Base
>      belongs_to :animal, :class_name => "Mod1::Animal"
>   end
> end
This topic is locked and can not be replied to.