ashbb / green_shoes

Green Shoes is one of the colorful Shoes written in pure Ruby.
Other
204 stars 37 forks source link

Green shoes seems to conflict with ActiveRecord models #56

Open translunar opened 13 years ago

translunar commented 13 years ago

I'm trying to use SciRuby::Plotter (a Green Shoes GUI) with Rails 3.1.1. I have a method on one of my models called update!, which sends an HTTP request to get some meta info, and then applies that meta info to the object (and saves). This happens within a transaction.

(1.0ms)  BEGIN
(1.3ms)  SELECT 1 FROM "genes" WHERE ("genes"."entrez_id" = 54101 AND "genes"."id" != 177592) LIMIT 1
(24.8ms)  UPDATE "genes" SET "symbol" = 'RIPK4', "name" = 'receptor-interacting serine-threonine kinase 4', "desc" = 'The protein encoded by this gene is a serine/threonine protein kinase that interacts with protein kinase C-delta. The encoded protein can also activate NFkappaB and is required for keratinocyte differentiation. This kinase undergoes autophosphorylation. [provided by RefSeq, Jul 2008]', "updated_at" = '2011-11-16 23:20:46.130810' WHERE "genes"."id" = 177592
(1.2ms)  COMMIT
NOTICE:  there is no transaction in progress
(1.1ms)  ROLLBACK
NoMethodError: undefined method `clear' for #<Gene:0xf954c28>
from /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/attribute_methods.rb:385:in `method_missing'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/attribute_methods.rb:60:in `method_missing'
from /usr/local/lib/ruby/gems/1.9.1/gems/green_shoes-1.0.309/lib/shoes/ruby.rb:155:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/green_shoes-1.0.309/lib/shoes/ruby.rb:155:in `clear'
from /usr/local/lib/ruby/gems/1.9.1/gems/green_shoes-1.0.309/lib/shoes/ruby.rb:155:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/green_shoes-1.0.309/lib/shoes/ruby.rb:155:in `clear'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:374:in `rollback_transaction_records'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:230:in `rescue in transaction'

I think there must be a conflict between lib/shoes/ruby.rb's Array modification and ActiveRecord.

I will try to gather some more data.

translunar commented 13 years ago

So it looks like ActiveRecord expects clear on Array to work differently. I was able to get rid of the aforementioned problem simply by renaming, in lib/shoes/ruby.rb, def clear to def green_shoes_clear, and def clear_all to def green_shoes_clear_all.

Of course, the problem with that solution is that now no Shoes app shows up at all. Not ideal.

ashbb commented 13 years ago

Hi John,

Thank you for the very helpful information! I should have had more consideration for overwriting Array#clear method. xx-P

This is a patch. Try it out.

class Array
  alias :_clear :clear
  def clear
    self.each{|e| e.clear if e.class.method_defined? :clear}
    _clear
  end
end

And... you have taught me another bug involved. Look at this commit: https://github.com/ashbb/green_shoes/commit/2e50b7ce3da408bb5f2951cd5d851a1f758ad091

ashbb