Killerpixler
Killerpixler

Reputation: 4050

Rails namespaced models give uninitialized constant errors

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

Upvotes: 0

Views: 728

Answers (1)

djaszczurowski
djaszczurowski

Reputation: 4515

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

Upvotes: 1

Related Questions