除非“包含”,否则不会加载活动记录关联。从原来的查找
为什么 client_destination 关联返回 nil,除非它是从原始模型查找中“包含”的!?
class Placement < ActiveRecord::Base
belongs_to :client_destination_type
belongs_to :client_destination,
:polymorphic => true,
:foreign_type => 'client_destination_type_class_name'
def client_destination_type_class_name
client_destination_type.class_name
end
end
不包含(损坏):
ruby-1.9.2-p0 :021 > Placement.last.client_destination
Placement Load (0.5ms) SELECT `placements`.* FROM `placements` WHERE (placements.deleted_at IS NULL) ORDER BY `placements`.`id` DESC LIMIT 1
=> nil
包含(有效):
ruby-1.9.2-p0 :022 > Placement.last(:include => :client_destination).client_destination
Placement Load (0.7ms) SELECT `placements`.* FROM `placements` WHERE (placements.deleted_at IS NULL) ORDER BY `placements`.`id` DESC LIMIT 1
ClientDestinationType Load (0.4ms) SELECT `client_destination_types`.* FROM `client_destination_types` WHERE `client_destination_types`.`id` = 2 LIMIT 1
ClientSite Load (0.4ms) SELECT `client_sites`.* FROM `client_sites` WHERE `client_sites`.`id` IN (3000018)
=> #<ClientSite id: 3000018, name: "WTF.com", created_at: "2011-08-09 12:23:51", updated_at: "2011-08-09 12:23:51", user_group_id: 23510, client_ad_tag_code_id: nil>
Why does the client_destination association return nil unless it was "included" from the original model look-up!?
class Placement < ActiveRecord::Base
belongs_to :client_destination_type
belongs_to :client_destination,
:polymorphic => true,
:foreign_type => 'client_destination_type_class_name'
def client_destination_type_class_name
client_destination_type.class_name
end
end
Without include (broken):
ruby-1.9.2-p0 :021 > Placement.last.client_destination
Placement Load (0.5ms) SELECT `placements`.* FROM `placements` WHERE (placements.deleted_at IS NULL) ORDER BY `placements`.`id` DESC LIMIT 1
=> nil
With include (works):
ruby-1.9.2-p0 :022 > Placement.last(:include => :client_destination).client_destination
Placement Load (0.7ms) SELECT `placements`.* FROM `placements` WHERE (placements.deleted_at IS NULL) ORDER BY `placements`.`id` DESC LIMIT 1
ClientDestinationType Load (0.4ms) SELECT `client_destination_types`.* FROM `client_destination_types` WHERE `client_destination_types`.`id` = 2 LIMIT 1
ClientSite Load (0.4ms) SELECT `client_sites`.* FROM `client_sites` WHERE `client_sites`.`id` IN (3000018)
=> #<ClientSite id: 3000018, name: "WTF.com", created_at: "2011-08-09 12:23:51", updated_at: "2011-08-09 12:23:51", user_group_id: 23510, client_ad_tag_code_id: nil>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法从您发布的内容中看出,但我猜想这是您在默认加载时行为不当的动态foreign_type选择器。
我猜测加载 client_destination_type 关联的操作顺序是为了生成其他关联中使用的 client_destination_type_class_name() 方法的值。
您可以通过换出:
并查看该静态类型的关联延迟加载是否正确来确认或否认这一点。
I can't tell from what you've got posted, but I would guess it's the dynamic foreign_type selector you've got misbehaving with the default loading.
I would guess an order of operations thing with the client_destination_type association being loaded to generate the value for client_destination_type_class_name() method used in the other association.
You could confirm or deny this by swapping out:
and seeing if the association lazy-loads correctly for that static type.
这修复了它(默认范围):
this fixed it (default scope):