开发者

Comparing two Activerecord::Base connections in Rails

What's the best way to compare connection information between two models?

I did an inspect on ActiveRecord::Base.connection and I saw that there's an @c开发者_C百科onfig variable. Would it be a good idea to retrieve that information and use it to compare?

Thanks!


If you just want to see if two models share the same connection, you can just use a equality comparison. I have a project right now with many models using one connection, and a couple of models that use a different connection (to a legacy database). Here's an IRB session:

#Two models that use the same connection
>> Committee.connection==StudentApplication.connection
=> true

#Two models that use different connections -- Term has "establish_connection XXX" in the class definition.
>> Committee.connection==Term.connection
=> false

(I think relying on an internal variable is almost always a bad idea -- it could change or behave in undocumented ways.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜