我的环境: Ruby 1.9.2p290,Rails 3.0.9和RubyGem 1.8.8
不幸的是,遇到多个数据库时我遇到了问题.
情况是这样的:我有两个模型连接两个不同的数据库,并建立彼此之间的关联.在每个模型中指定数据库连接,看起来像
class Visit < ActiveRecord::Base
self.establish_connection "lab"
belongs_to :patient
end
class Patient < ActiveRecord::Base
self.establish_connection "main"
has_many :visits
end
Run Code Online (Sandbox Code Playgroud)
遇到以下情况时我遇到了错误
@visits = Visit.joins(:patient)
Run Code Online (Sandbox Code Playgroud)
错误:Mysql2 ::错误:表'lab.patients'不存在:SELECT visits.*FROM visitsINNER JOIN patientsON patients.id一片空白
这里'患者'表在'主'数据库和'实验室'数据库中的'访问'表我怀疑执行代码时,Rails正在考虑'患者'表是'实验室'数据库的一部分[持有'访问'表].
我有类别模型和类别有很多帖子.
问题:有时在Web中的类别下不会显示过帐,即使数据库中存在记录
我通过启用config.log_level =调试并重新启动nginx乘客服务器来调查生产环境中的操作查询.现在我可以看到该类别下的记录.我无法再次重现同样的问题,很少发生.
注意:
型号如下
class Category < ActiveRecord::Base
has_many :postings, conditions: ['paid = ? AND start_date <= ? AND end_date >= ?', true, Date.current, Date.current]
end
class Posting < ActiveRecord::Base
searchkick
belongs_to :category
class << self
def payed
where paid: true
end
def activated
where :code => ""
end
def starts_on(date)
where "start_date <= ?", date
end
def ends_after(date)
where "end_date >= ?", date
end
def in_location(state,city)
where(stateid: state.id, cityid: city.id)
end
def not_deleted
where …Run Code Online (Sandbox Code Playgroud) 有没有办法避免在分配集合属性时自动保存对象(collection_singular_ids = ids方法)?
例如,我有以下测试和包模型,包有很多测试.用户可以使用多个测试构建包捆绑包.
# test.rb
class Test < ActiveRecord::Base
end
# package.rb
class Package < ActiveRecord::Base
has_many :package_tests
has_many :tests, :through => :package_tests
belongs_to :category
validate :at_most_3_tests
private
# tests count will differ depends on category.
def at_most_3_tests
errors.add(:base, 'This package should have at most three tests') if test_ids.count > 3
end
end
# package_test.rb
class PackageTest < ActiveRecord::Base
belongs_to :package
belongs_to :test
validates_associated :package
end
Run Code Online (Sandbox Code Playgroud)
当包对象是新的时,验证没有问题.
1.9.2 :001> package = Package.new(:name => "sample", :cost => 3.3, …Run Code Online (Sandbox Code Playgroud) 如果用户在对话框外单击,引导模式中是否有触发事件的选项?
例如:当用户单击远离模态时,我想添加动画抖动css。