【问题标题】:Rails 4: Use MySql and MongoDB togetherRails 4:一起使用 MySql 和 MongoDB
【发布时间】:2015-11-07 14:50:24
【问题描述】:

我正在尝试一起使用 MongoDB(mongoid) 和 MySQL 在 Rails 4 中创建一个应用程序。 但我无法设置它。

我正在按照以下步骤操作:

  1. rails new myapp -d mysql
  2. 然后将这些行添加到 Gemfile:
    gem "mongoid"
    gem "bson_ext"
  3. bundle install
  4. rails g mongoid:config

第 4 步失败。我无法弄清楚问题所在。它显示以下错误。

/home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations.rb:5:in require': /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations/has_many.rb:79: syntax error, unexpected keyword_do_cond, expecting ':' @documents = attributes ? attributes.collect do |attrs| ^ /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations/has_many.rb:84: syntax error, unexpected ':', expecting keyword_end end : [] ^ /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations/has_many.rb:99: syntax error, unexpected keyword_do_cond, expecting keyword_end attributes.values.each do |attrs| ^ /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations/has_many.rb:139: syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError) from /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations.rb:5:in' 来自/home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid.rb:40:in require' from /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid.rb:40:in' 来自 /home/devesh/.rvm/gems/ruby-2.2.2/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in require' from /home/devesh/.rvm/gems/ruby-2.2.2/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:inblock (2 个级别) in require' 来自 /home/devesh/.rvm/gems/ruby-2.2.2/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:in each' from /home/devesh/.rvm/gems/ruby-2.2.2/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:inblock in require' 来自/home/devesh/.rvm/gems/ruby-2.2.2/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in each' from /home/devesh/.rvm/gems/ruby-2.2.2/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:inrequire' 来自/home/devesh/.rvm/gems/ruby-2.2.2/gems/bundler-1.10.6/lib/bundler.rb:134:in require' from /home/devesh/fashion_hub/store/config/application.rb:7:in' 来自 /home/devesh/.rvm/gems/ruby-2.2.2/gems/spring-1.3.6/lib/spring/application.rb:82:in require' from /home/devesh/.rvm/gems/ruby-2.2.2/gems/spring-1.3.6/lib/spring/application.rb:82:inpreload' 来自 /home/devesh/.rvm/gems/ruby-2.2.2/gems/spring-1.3.6/lib/spring/application.rb:143:in serve' from /home/devesh/.rvm/gems/ruby-2.2.2/gems/spring-1.3.6/lib/spring/application.rb:131:inblock in run' 来自/home/devesh/.rvm/gems/ruby-2.2.2/gems/spring-1.3.6/lib/spring/application.rb:125:in loop' from /home/devesh/.rvm/gems/ruby-2.2.2/gems/spring-1.3.6/lib/spring/application.rb:125:inrun' 来自/home/devesh/.rvm/gems/ruby-2.2.2/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in <top (required)>' from /home/devesh/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' 来自 /home/devesh/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'

我关注了这个url。

一些细节:

  1. 操作系统 - ubuntu 14.04
  2. ruby v2.2.2
  3. 导轨 4.2.3

【问题讨论】:

    标签: mysql mongodb ruby-on-rails-4 mongoid4


    【解决方案1】:

    将这些行添加到您的 application.rb 文件中:
    config.generators do |g| g.orm :active_record end

    这将强制 rails g 使用活动记录。

    每当您想为 mongodb 生成模型时,请使用:
    rails g mongoid:model yourmodelname

    【讨论】:

    • 那么我将如何在 mongoid 中创建任何模型?
    猜你喜欢
    • 2018-07-21
    • 2014-10-19
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2022-06-22
    • 2011-10-14
    相关资源
    最近更新 更多