好的,这是一个非常奇怪的问题。我试着写,将延长的ActiveRecord ::迁移库,这样我可以在我的Rails迁移写这样的代码:Rails将选项散列成字符串
class TestEnterprise < ActiveRecord::Migration
def up
enterprise_mti_up superclass_table: 'test_superclasses', subclass_tables: ['test_subclass_ones', 'test_subclass_twos']
end
def down
enterprise_mti_down superclass_table: 'test_superclasses', subclass_tables: ['test_subclass_ones', 'test_subclass_twos']
end
end
这里是库代码的样本:
def enterprise_mti_up(*args)
enterprise_mti args.extract_options!, direction: :up
end
def enterprise_mti_down(*args)
enterprise_mti args.extract_options!, direction: :down
end
当我在任何一个方向运行迁移,一切都显得向工作:
== TestEnterprise: migrating =================================================
-- enterprise_mti_up({:superclass_table=>"test_superclasses", :subclass_tables=>["test_subclass_ones", "test_subclass_twos"]})
-> 0.0005s
== TestEnterprise: migrated (0.0007s) ========================================
但数据库保持不变,因为事实上Rails是一些如何将选项从enterprise_mti_up和enterprise_mti_down转换为字符串!当我改变的功能之一操纵散列,我得到如下结果:
def enterprise_mti_down(*args)
opts = args.extract_options!
puts "opts: #{opts}"
puts "opts[:superclass_table]: #{opts[:superclass_table]}"
puts "args: #{args}"
puts "args.last.class: #{args.last.class}"
enterprise_mti args.extract_options!, direction: :down
end
...
== TestEnterprise: reverting =================================================
-- enterprise_mti_down({:superclass_table=>"test_superclasses", :subclass_tables=>["test_subclass_ones", "test_subclass_twos"]})
opts: {}
opts[:superclass_table]:
args: ["{:superclass_table=>\"test_superclasses\", :subclass_tables=>[\"test_subclass_ones\", \"test_subclass_twos\"]}"]
args.last.class: String
-> 0.0002s
== TestEnterprise: reverted (0.0005s) ========================================
有没有人有,为什么哈希被转换为字符串的任何想法,我怎么能传递一个哈希我的方法?谢谢!
注意:在我的测试中,我发现如果我在选项散列之前传递一个字符串作为第一个参数,那么一切都按照它应该的方式工作。但是我不应该在散列之前有任何争论。这导致我认为Rails可能会被硬连接,以期将字符串/符号作为迁移方法的第一个参数。
由于您修改过的'enterprise_mti_down'版本因为它调用'extract_options!'两次而被破坏,'extract_options!'是'Array#pop'周围非常薄的包装。这就是说,我没有看到和你一样的行为。 –
尝试替换args.extract_options!通过args.first – carlosavoy
良好的捕获,@ muistooshort。 – earksiinni