Gmail ͏°´õ ¹®¼­µµ±¸ ¸®´õ Google °Ë»ö ´õº¸±â »
ÃÖ±Ù ¹æ¹®ÇÑ ±×·ì | µµ¿ò¸» | ·Î±×ÀÎ
Google ±×·ì Ȩ
Rails 2.1 and const_missing Åä·ÐÀ¸·ÎºÎÅÍ ¿Â ¸Þ½ÃÁö
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ½Ç ±×·ìÀº À¯Áî³Ý ±×·ìÀÔ´Ï´Ù. ÀÌ ±×·ì¿¡ ¸Þ½ÃÁö¸¦ °Ô½ÃÇϽøé ÀÎÅͳÝÀÇ ¸ðµç »ç¿ëÀÚ°¡ »ç¿ëÀÚÀÇ À̸ÞÀÏÀ» º¼ ¼öµµ ÀÖ½À´Ï´Ù.
´ä±ÛÀÌ Àü¼ÛµÇÁö ¾Ê¾Ò½À´Ï´Ù.
°Ô½ÃµÇ¾ú½À´Ï´Ù.
 
º¸³½»ç¶÷:
¹Þ´Â»ç¶÷:
ÂüÁ¶:
Ãß°¡ ´äº¯:
ÂüÁ¶ Ãß°¡ | Ãß°¡ ´äº¯ Ãß°¡ | Á¦¸ñ ¼öÁ¤
Á¦¸ñ:
È®ÀÎ:
È®ÀÎÀ» À§ÇØ ¾Æ·¡ ±×¸²¿¡ Ç¥½ÃµÈ ¹®ÀÚ¸¦ ÀÔ·ÂÇϰųª ¾×¼¼½º ¾ÆÀÌÄÜÀ» Ŭ¸¯ÇÏ¿© µé¸®´Â ¼ýÀÚ¸¦ ÀÔ·ÂÇϼ¼¿ä. ¼ýÀÚ¸¦ µéÀº ÈÄ ÇØ´ç ¼ýÀÚ¸¦ ÀÔ·ÂÇϼ¼¿ä.
 
Phillip Koebbe  
ÇÁ·ÎÇÊ º¸±â   ·Î ¹ø¿ª ¹ø¿ªµÊ(¿ø¹® º¸±â)
 Ãß°¡ ¿É¼Ç 2008³â9¿ù21ÀÏ, ¿ÀÀü10½Ã51ºÐ
º¸³½»ç¶÷: Phillip Koebbe <phillipkoe...@gmail.com>
³¯Â¥: Sat, 20 Sep 2008 18:51:26 -0700 (PDT)
ÇöÁö½Ã°£: 2008³â9¿ù21ÀÏ(ÀÏ) ¿ÀÀü10½Ã51ºÐ
Á¦¸ñ: Rails 2.1 and const_missing
I was using MMC in a rake task in Rails 2.0.2 successfully, but when I
recently upgraded to Rails 2.1.1, the rake task failed with the error

uninitialized constant {const_id}

A little bit of digging turned up a couple of things.

1) There is a type in connected.rb at line 20:

raise NameError.new("uninitialized constant \{const_id}") unless
target_class

Notice the backslash where a pound should be. The reason this was
never found was because

2) ActiveSupport::Inflector.constantize was changed between 2.0.2 and
2.1 such that const_missing is actually called now. In 2.0.2, it was
defined as

  def constantize(camel_cased_word)
    unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
      raise NameError, "#{camel_cased_word.inspect} is not a valid
constant name!"
    end

    Object.module_eval("::#{$1}", __FILE__, __LINE__)
  end

Notice that it's doing a regex match on the format of camel_case_word,
not really checking to see if it exists. In 2.1, it's defined as

    def constantize(camel_cased_word)
      names = camel_cased_word.split('::')
      names.shift if names.empty? || names.first.empty?

      constant = Object
      names.each do |name|
        constant = constant.const_defined?(name) ?
constant.const_get(name) : constant.const_missing(name)
      end
      constant
    end

Notice here that if the constant is not defined, the object's
const_missing method will be called, which brings us back to
connected.rb. Since in 2.0.2 const_missing didn't get called, I
figured it probably wouldn't hurt to just return if the const doesn't
exist, so I changed

raise NameError.new("uninitialized constant #{const_id}") unless
target_class

to

return unless target_class

I then ran my rake task in Rails 2.1.1 and it seemed to work
correctly. I don't know if this is appropriate or not, but hey it
seems to work.

Peace,
Phillip


    Àü´Þ  
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ±×·ì¿¡ °¡ÀÔÇØ¾ß ÇÕ´Ï´Ù.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ¸ÕÀú ÇØ´ç ±×·ì¿¡ °¡ÀÔÇÏ¼Å¾ß ÇÕ´Ï´Ù.
°Ô½ÃÇϱâ Àü¿¡ °¡ÀÔ ¼³Á¤ ÆäÀÌÁö¿¡¼­ ´Ð³×ÀÓÀ» ¾÷µ¥ÀÌÆ®Çϼ¼¿ä.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÒ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.

±×·ì ¸¸µé±â - Google ±×·ì½º - Google Ȩ - ¼­ºñ½º ¾à°ü - °³ÀÎÁ¤º¸ º¸È£Á¤Ã¥
©2010 Google