Forum: Ruby on Rails X is not missing constant Y

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.
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2008-10-30 15:14
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?
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2008-10-30 15:26
(Received via mailing list)
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.
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2008-10-30 15:35
Ryan Bigg 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 :)

Thanks.
44a43e7fef8e933e802a7802b4bd3525?d=identicon&s=25 John Small (johnsmall)
on 2008-10-30 15:39
Jamal Soueidan 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 Small
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2008-10-30 16:58
(Received via mailing list)
Well don't leave us hanging--how'd you fix it? ;-)
44a43e7fef8e933e802a7802b4bd3525?d=identicon&s=25 John Small (johnsmall)
on 2008-10-30 20:08
Roy Pardee wrote:
> Well don't leave us hanging--how'd you fix it? ;-)

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 Small
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-30 20:28
(Received via mailing list)
On Oct 30, 7:08 pm, John Small <rails-mailing-l...@andreas-s.net>
wrote:
> Roy Pardee wrote:
> > Well don't leave us hanging--how'd you fix it? ;-)
>
> 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
44a43e7fef8e933e802a7802b4bd3525?d=identicon&s=25 John Small (johnsmall)
on 2008-10-30 21:01
Frederick Cheung 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 Small
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2008-10-30 21:43
Roy Pardee wrote:
> Well don't leave us hanging--how'd you fix it? ;-)

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 :)

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 :)
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-30 23:22
(Received via mailing list)
On Oct 30, 8:01 pm, John Small <rails-mailing-l...@andreas-s.net>
wrote:
> Frederick Cheung 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
44a43e7fef8e933e802a7802b4bd3525?d=identicon&s=25 John Small (johnsmall)
on 2008-10-31 09:52
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 Small

Jamal Soueidan wrote:
> Roy Pardee wrote:
>> Well don't leave us hanging--how'd you fix it? ;-)
>
> 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 :)
>
> 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 :)
This topic is locked and can not be replied to.