X is not missing constant Y


#1

Moving my app from rails 1.2 to rails 2.1, I stuck into this weird
problem?

X is not missing constant Y.

The problem occurs when I use the join table association.

X.find(1).y

It works in (script/console) but not in browser?

Anyone have any experience with this issue?


#2

It would be better if you explained what X and Y were and how they
relate to each other. Could you please do that for us? Thanks.


#3

Ryan B. wrote:

It would be better if you explained what X and Y were and how they
relate to each other. Could you please do that for us? Thanks.

It’s fixed :slight_smile:

Thanks.


#4

Jamal S. wrote:

Moving my app from rails 1.2 to rails 2.1, I stuck into this weird
problem?

X is not missing constant Y.

The problem occurs when I use the join table association.

X.find(1).y

It works in (script/console) but not in browser?

Anyone have any experience with this issue?

Yes! and the really bizarre part of the problem is that the same Rails
version worked OK in WinXP

My problem appeared when I had the following in let’s say
legacy_data_models.rb

module LegacyDataBaseStuff
$olddb = {some_db_options}

Class OldParentItem < ActiveRecord::Base
has_many :old_child_items
establish_connection $olddb
end

Class OldChildItem < ActiveRecord::Base
belongs_to :old_parent_item
establish_connection $olddb
end

end

Then in a rake task to import things from a legacy database I’d open a
parent like so

parent = LegacyDataBaseStuff::OldParentItem.find :all

which works until I’d try

parent.old_child_items

which would give me exactly the error you’re getting “X is not missing
constant Y”

I worked around the problem by taking the models out of the module and
just putting everything in the rake file. Not clean but it did the job
and the job only needed to be done once.

I’d be interested to know how your situation compares

Cheers

John S.


#5

Well don’t leave us hanging–how’d you fix it? :wink:


#6

On Oct 30, 7:08 pm, John S. removed_email_address@domain.invalid
wrote:

Roy P. wrote:

Well don’t leave us hanging–how’d you fix it? :wink:

Oh I thought I’d explained it in the previous message. I took the class
definitions out of the module, and moved them into the rake file. That
did the trick.

If you were previously using require to require files in your app then
that can screw things up big time.

Fred


#7

Frederick C. wrote:

If you were previously using require to require files in your app then
that can screw things up big time.

Fred

A very mysterious clue! How would require mess things up? This is
something we all need to know. I certainly do, it’s quite hard for me to
find out about all the little hidden undocumented tricks that one needs
to know to become a true pro.

Cheers

John S.


#8

Roy P. wrote:

Well don’t leave us hanging–how’d you fix it? :wink:

Oh I thought I’d explained it in the previous message. I took the class
definitions out of the module, and moved them into the rake file. That
did the trick.

I suspect that the problem has something to do with the associations
being to ActiveRecords that are declared inside modules and then used
outside those modules. But I’ve not had a chance to play around to find
out.

John S.


#9

On Oct 30, 8:01 pm, John S. removed_email_address@domain.invalid
wrote:

Frederick C. wrote:

If you were previously using require to require files in your app then
that can screw things up big time.

Fred

A very mysterious clue! How would require mess things up? This is
something we all need to know. I certainly do, it’s quite hard for me to
find out about all the little hidden undocumented tricks that one needs
to know to become a true pro.

Short version: because requiring files that could be loaded by rails’
dependency system confuses said system (in development mode)
long version: http://www.spacevatican.org/2008/9/28/required-or-not

Fred


#10

Excellent explanation, I guessed it must be down to something like that
but didn’t have time to muck around to find the correct solution so I
bodged it.

I think it might be a good idea to try and get your solution integrated
into the api documentation. I’ve googled around and found other people
wrestling with this issue and there will be more in the future. It’s
nice to have a clear explanation in the official docs.

John S.

Jamal S. wrote:

Roy P. wrote:

Well don’t leave us hanging–how’d you fix it? :wink:

Sorry, I went home from work before but now I’m online again.

class Blog::Post < ActiveRecord::Base
belongs_to :category
end

class Blog::Category < ActiveRecord::Base
has_many :posts
end

Error:

Blog is not missing constant Post

so I just did this :slight_smile:

class Blog::Post < ActiveRecord::Base
belongs_to :category, :class_name => “Blog::Category”, :foreign_key =>
:category_id
end

class Blog::Category < ActiveRecord::Base
has_many :posts, :class_name => “Blog::Post”, :foreign_key =>
:category_id
end

and then it works :slight_smile:


#11

Roy P. wrote:

Well don’t leave us hanging–how’d you fix it? :wink:

Sorry, I went home from work before but now I’m online again.

class Blog::Post < ActiveRecord::Base
belongs_to :category
end

class Blog::Category < ActiveRecord::Base
has_many :posts
end

Error:

Blog is not missing constant Post

so I just did this :slight_smile:

class Blog::Post < ActiveRecord::Base
belongs_to :category, :class_name => “Blog::Category”, :foreign_key =>
:category_id
end

class Blog::Category < ActiveRecord::Base
has_many :posts, :class_name => “Blog::Post”, :foreign_key =>
:category_id
end

and then it works :slight_smile: