如何使用RoR和mongodb动态创建模型?

时间:2017-03-13 17:41:37

标签: ruby-on-rails mongodb mongoid metaprogramming

我正在尝试创建一个动态应用程序来创建通过元编程MVC所需的任何类型的数据我试过这个模型:

class DynamicRecord

    attr_accessor :name, :attributes

    def initialize(name, attributes = [])
        raise "Error: Constant #{name} already in namespace" if name.in? Object.constants
        a_new_class = Class.new(Object) do |clazz|
            include Mongoid::Document
            attributes.map do |attribute|
                field attribute[:name], type: attribute[:type]
            end
        end
        Object.const_set(name, a_new_class) 
    end
end

DynamicRecord.new('Person', [{name: :name, type: String}, {name: :email, type: String}])

person = Person.new(name: "Foo", email: "Foo@foo.com")
person.save

然后我收到此错误:

Mongo::Error::OperationFailure: Invalid ns [mongodb_divcad_development.] (16257)
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/result.rb:256:in `validate!'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/write/insert.rb:60:in `block in execute_message'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/server/connection_pool.rb:107:in `with_connection'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/server.rb:242:in `with_connection'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/write/insert.rb:59:in `execute_message'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/write/write_command_enabled.rb:39:in `execute'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/collection.rb:365:in `block in insert_one'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/retryable.rb:112:in `write_with_retry'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/collection.rb:356:in `insert_one'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/query_cache.rb:182:in `insert_one_with_clear_cache'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:79:in `insert_as_root'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:27:in `block in insert'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:118:in `block (2 levels) in prepare_insert'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__4510143668266298615__create__callbacks'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/interceptable.rb:138:in `run_callbacks'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:117:in `block in prepare_insert'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__4510143668266298615__save__callbacks'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/interceptable.rb:138:in `run_callbacks'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:116:in `prepare_insert'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:23:in `insert'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/savable.rb:23:in `save'
    from (irb):70
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
    from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

这可以吗?有一种更好的方法,而不属于Entity-attribute-value模型?

2 个答案:

答案 0 :(得分:3)

当您尝试使用动态模型时,Mongoid不了解命名空间。要修复它,您应该使用 store_in

设置名称空间

示例:

class DynamicCollection
  def self.create(collection, fields)
    klass = Class.new do
      include Mongoid::Document
      store_in collection: collection.downcase

      fields.each do |item|
        field item[:name], type: item[:type]
      end
    end

    Object.const_set(collection, klass) 
  end
end

fields = [
  {name: 'name', type: String},
  {name: 'email', type: String}
]

DynamicCollection.create('Demo', fields)

Demo.create!(name: 'SomeValue', email: 'SomeValue')

答案 1 :(得分:0)

我没有在本地尝试这个,这可能是一个愚蠢的答案......但我会改变

attributes.map到attributes.each似乎map会返回一个数组并导致错误。

...还 https://github.com/mongodb/mongoid/blob/master/lib/mongoid/fields.rb#L335

查看mongoid的来源。您可以使用一种名为add_field的方法。