# Should this be considered a bug?

**URL:** <https://www.ruby-forum.com/t/should-this-be-considered-a-bug/48385>\
**Category:** Rails\
**Created:** [November 19, 2005, 7:26pm UTC](https://www.ruby-forum.com/t/should-this-be-considered-a-bug/48385 "2005-11-19T19:26:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_SS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@Rick\_SS](https://www.ruby-forum.com/u/Rick_SS)\
**Post date:** [November 19, 2005, 7:26pm UTC](https://www.ruby-forum.com/t/should-this-be-considered-a-bug/48385/1 "2005-11-19T19:26:00Z")

</div>

If you define a ruby program with only one model class like this:

class Order \< ActiveRecord::Base  
has\_many :legs  
end

and there are no other models defined shouldn’t the has\_many cause a  
compile error since there is no other model defined to which the  
association can be applied?

Rick

---

<div class="post-metadata">

**Author:** ![Rick\_SS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@Rick\_SS](https://www.ruby-forum.com/u/Rick_SS)\
**Post date:** [November 19, 2005, 7:57pm UTC](https://www.ruby-forum.com/t/should-this-be-considered-a-bug/48385/2 "2005-11-19T19:57:17Z")

</div>

On Nov 19, 2005, at 11:26 AM, Rick wrote:

> If you define a ruby program with only one model class like this:
> 
> class Order \< ActiveRecord::Base  
> has\_many :legs  
> end
> 
> and there are no other models defined shouldn’t the has\_many cause a  
> compile error since there is no other model defined to which the  
> association can be applied?

No, because if we checked at parse time, there would be no way to  
handle circular dependencies, like this:

class Order \< ActiveRecord::Base  
has\_many :legs  
end

class Leg \< ActiveRecord::Base  
belongs\_to :order  
end

When the has\_many instruction is parsed, the Leg class has not yet  
been seen. Thus, things have to be loaded lazily, such that errors  
caused by missing classes won’t be reported until an attempt to  
actually instantiate the class is made.

- Jamis

---

<div class="post-metadata">

**Author:** ![Rick\_SS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@Rick\_SS](https://www.ruby-forum.com/u/Rick_SS)\
**Post date:** [November 20, 2005, 4:59pm UTC](https://www.ruby-forum.com/t/should-this-be-considered-a-bug/48385/3 "2005-11-20T16:59:25Z")

</div>

Rick wrote:

> Shouldn’t the compiler at least detect the attempt to define the same  
> class a second time?
> 
> Rick

Ruby has no problem about spcifyinbg the same class several times…many  
people actually considers this a feature 😉

---

<div class="post-metadata">

**Author:** ![Rick\_SS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@Rick\_SS](https://www.ruby-forum.com/u/Rick_SS)\
**Post date:** [November 20, 2005, 5:13pm UTC](https://www.ruby-forum.com/t/should-this-be-considered-a-bug/48385/4 "2005-11-20T17:13:20Z")

</div>

On 11/20/05, Rick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Shouldn’t the compiler at least detect the attempt to define the same  
> class a second time?

Absolutely not, that’s one of the greatest features IMO of ruby - the  
fact  
that a class/object is never closed. I’m not sure what your level of  
ruby  
experience is - but you aren’t redefining your class in the example you  
quote - you are adding to the class definition. I’m not quite sure what  
happens under the covers with activerecord when you duplicate the  
has\_many  
attribute but I would imagine it’s probably quite benign (hopefully a  
hash  
or something that wouldn’t have a problem with a duplicate member).

You can modify a class from anywhere at all in your code. It means that  
if  
you wanted to add something to the String class you need not modify the  
core  
libraries - you can just add the method in your code and they will get  
added. There is the freeze method for objects which sort of prevents you  
from making modifications but for all real purposes ruby doesn’t lock  
down  
objects at all. While it may seem a little strange at first - boy does  
it  
get nice as you start to see the ways it can help you.

---

<div class="post-metadata">

**Author:** ![Rick\_SS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@Rick\_SS](https://www.ruby-forum.com/u/Rick_SS)\
**Post date:** [November 20, 2005, 4:55pm UTC](https://www.ruby-forum.com/t/should-this-be-considered-a-bug/48385/5 "2005-11-20T16:55:27Z")

</div>

Seems that a post-compile model check could be made.

OK, is this a bug? Compile this code:

require ‘rubygems’  
require\_gem ‘activerecord’

class Order \< ActiveRecord::Base  
has\_many :legs  
end

class Order \< ActiveRecord::Base  
has\_many :legs  
end

Shouldn’t the compiler at least detect the attempt to define the same  
class a second time?

Rick
