Forum: Ruby on Rails Should this be considered a bug?

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.
01ec89b682c7ae076d25c39a70def227?d=identicon&s=25 Rick <willirl@gmail.com> (Guest)
on 2005-11-19 19:26
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
992fe8c19bbbc27f2b562a9f96efc03d?d=identicon&s=25 jamis (Guest)
on 2005-11-19 19:57
(Received via mailing list)
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
01ec89b682c7ae076d25c39a70def227?d=identicon&s=25 Rick (Guest)
on 2005-11-20 16:55
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
C7669e8b5676f61fdf202230cbcf72d8?d=identicon&s=25 mikkel (Guest)
on 2005-11-20 16:59
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 ;-)
60b83bb4579cd684731a1709f85ee849?d=identicon&s=25 wishdev (Guest)
on 2005-11-20 17:13
(Received via mailing list)
On 11/20/05, Rick <willirl@gmail.com> 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.
This topic is locked and can not be replied to.