我有两个ActiceRecord类:如何找到对象关联的类?
class A < ActiveRecord::Base
has_one :b
end
class B < ActiveRecord::Base
belongs_to :a
end
如何确定到其中B相关当A和B之间的关联是唯一例如初始化为对象在运行时?
我期待这样的事情:
b.association => A
我有两个ActiceRecord类:如何找到对象关联的类?
class A < ActiveRecord::Base
has_one :b
end
class B < ActiveRecord::Base
belongs_to :a
end
如何确定到其中B相关当A和B之间的关联是唯一例如初始化为对象在运行时?
我期待这样的事情:
b.association => A
我不知道这是否会帮助你,但
B.reflections
...会给你的职业等级协会的哈希值。你可以通过这些过滤。
例如:
class Datastream < ActiveRecord::Base
belongs_to :dashboard
end
Datastream.reflections
# outputs: {:dashboard=>#<ActiveRecord::Reflection::AssociationReflection:0x00000100e21ef8 @macro=:belongs_to, @name=:dashboard, @options={}, @active_record=Datastream(id: integer, dashboard_id: integer, position: integer, feed_id: string, stream_id: string, created_at: datetime, updated_at: datetime), @collection=false>}
真脏方式:
`cat app/models/b.rb | grep 'belongs_to'`.gsub("belongs_to ", "")
只要避免使用动态数据,以避免:
; rm -rf *
被插在那里。 ...
哈哈哈,棘手:)),但我喜欢它。 – 2011-03-18 13:14:42
是的,这是我一直在寻找的。万分感谢! – auralbee 2011-03-18 14:27:18