Should this be considered a bug?

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

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

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

On 11/20/05, Rick [email protected] 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.

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