0

I have two models in namespace project like this: class Project::Foo and class Project::Bar

They have this relationship

in app/models/project/foo.rb has_many :bars

in app/models/project/bar.rb belongs_to :foo

However when I want to call Project::Foo.create(...) or even Project::Bar.create(...) I get a NameError with uninitialized constant Foo or Bar respectively.

Do I need to put something like this in the models? belongs_to :project::foo? or how do I fix this?

EDIT

in app/models/project/foo.rb now reads:

module Project
  class Foo
    has_many :bars
  end
end

and bars has the same structure but with the belongs_to in it

I still get the same error

Killerpixler
  • 4,200
  • 11
  • 42
  • 82
  • Do you get the error if you remove the `belongs_to` and `has_many` relationships? – Helios de Guerra May 15 '15 at 04:09
  • Are your models inheriting from ActiveRecord? I don't think you can use the relationship macros w/o that. So if you change your class definition to `class Foo < ActiveRecord::Base` (inside your module def)? – Helios de Guerra May 15 '15 at 14:05
  • no they do not (am not using AR), however inside the class they use `include Mongoid::Document`. This used to work fine before the namespacing. – Killerpixler May 15 '15 at 14:29
  • Seeing it was Mongoid that has the issue, [this](https://stackoverflow.com/questions/19211962/what-is-the-proper-way-to-use-namespaces-and-reference-models-inheriting-from-ot) fixed it – Killerpixler May 15 '15 at 14:32

1 Answers1

1

if you have class Foo, code within the file should look

module Project #create the scope
  class Foo

  end
end 

or you may, define file project.rb with

module Project
end

and keep related models in project folder

djaszczurowski
  • 4,385
  • 1
  • 18
  • 28