小编Pau*_*aul的帖子

使用不同模式的多个数据库的Rake任务

我正在开发一个多数据库Rails 3应用程序.每个数据库都有不同的模式(并且在生产中位于不同的位置).我已经将应用程序设置为与不同的数据库通信,如下所示:

database.yml的

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: main_development
  pool: 5
  username: someuser
  password: somepassword
  socket: /tmp/mysql.sock

other_development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: other_development
  pool: 5
  username: someuser
  password: somepassword
  socket: /tmp/mysql.sock
Run Code Online (Sandbox Code Playgroud)

车型/ other_base.rb

class OtherBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "other_#{Rails.env}"
end
Run Code Online (Sandbox Code Playgroud)

车型/ some_model.rb

class SomeModel < OtherBase
  # Regular stuff here
end
Run Code Online (Sandbox Code Playgroud)

现在,这适用于Web应用程序,但不太适合运行rake任务,包括测试(夹具未正确加载).有没有可用的宝石?任何帮助赞赏.

此外,创建一个可以处理不同数据库的不同模式的schema.rb文件会很好 - 也就是说,允许我执行rake db:create或db:setup之类的操作并让它使用数据库创建多个数据库特定的架构.

ruby-on-rails rake-task

9
推荐指数
1
解决办法
4200
查看次数

如何在rails 3中自动生成相关的多态activerecord对象

class ItemSource < ActiveRecord::Base
  belongs_to :product, :polymorphic => true
end

class RandomProduct < ActiveRecord::Base
  has_one :item_source, :as => :product, :autosave => true, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud)

我想做的是打电话:

a = RandomProduct.find(1)
a.item_source
Run Code Online (Sandbox Code Playgroud)

如果item_source尚不存在(= nil),则自动构建(build_item_source).

以前,我用alias_chain_method做了这个,但是在Rails 3中不支持.
哦,我也试过这个无济于事:

class RandomProduct < ActiveRecord::Base
  has_one :item_source, :as => :product, :autosave => true, :dependent => :destroy

  module AutoBuildItemSource
    def item_source
      super || build_item_source
    end
  end  
  include AutoBuildItemSource
end
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails-3

7
推荐指数
1
解决办法
3656
查看次数