我正在使用此gem(https://github.com/latortuga/has_magic_columns)来允许我的客户在每个帐户级别定义自定义属性。例如对于Orders对象,客户A可以添加“field1”,“field2”作为属性,而客户B可以添加“field3”和“field4”,而不必将它们作为实际列添加到订单表。
这个宝石停止工作通过rails 3.我下载了宝石代码,并修复了一些小问题,并升级了gemspec以支持rails 4.2,但我坚持核心问题,这反映在spec故障中比如这个:
UPDATED StackTrace:
1) HasMagicColumns on a single model allows setting and saving of magic attributes
SystemStackError:
stack level too deep
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/associations/through_association.rb:6:in `through_reflection'
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/associations/through_association.rb:68:in `stale_state'
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/associations/association.rb:76:in `stale_target?'
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:32:in `reader'
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/associations/builder/association.rb:115:in `magic_attributes'
# ./lib/has_magic_columns/active_record.rb:162:in `find_magic_attribute_by_column'
# ./lib/has_magic_columns/active_record.rb:142:in `block in initialize_magic_columns'
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
# ./lib/has_magic_columns/active_record.rb:141:in `initialize_magic_columns'
# ./lib/has_magic_columns/active_record.rb:113:in `rescue in method_missing'
# ./lib/has_magic_columns/active_record.rb:107:in `method_missing'
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/persistence.rb:240:in `update_attribute'
# ./lib/has_magic_columns/active_record.rb:154:in `block in initialize_magic_columns'
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
# /Users/testuser/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
答案 0 :(得分:2)
宝石很老,没有维护。正如自述文件所述,它最初是作为Rails 2插件编写的,然后打包为用于Rails 3的gem。
除非您计划进行重大改写,否则我不建议只是“压缩”测试错误并使其发挥作用"。
相反,为什么不使用Rails 4.2提供的新功能?
如果您正在使用PostgreSQL,Rails 4支持hstore
和JSON
等原生列类型:
http://guides.rubyonrails.org/v4.2/active_record_postgresql.html
如果你不在PostgreSQL上,你仍然可以使用序列化属性: http://api.rubyonrails.org/v4.2/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html#method-i-serialize
如果真的需要一个外观界面,可以提供真实模型属性的幻觉,您可以使用method_missing
轻松完成它,或者您可以重新实现我所需的宝石#&# 39;上面有联系。
我不认为你需要它们,因为如果你的客户可以定义自定义属性,那么我不认为你的代码在任何地方都有硬编码。