除非“包含”,否则不会加载活动记录关联。从原来的查找

发布于 2025-01-08 17:34:44 字数 1358 浏览 0 评论 0原文

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2025-01-15 17:34:44

我无法从您发布的内容中看出,但我猜想这是您在默认加载时行为不当的动态foreign_type选择器。

我猜测加载 client_destination_type 关联的操作顺序是为了生成其他关联中使用的 client_destination_type_class_name() 方法的值。

您可以通过换出:

belongs_to :client_destination,
  :polymorphic => true,
  :foreign_type => 'ClientSite'

并查看该静态类型的关联延迟加载是否正确来确认或否认这一点。

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:

belongs_to :client_destination,
  :polymorphic => true,
  :foreign_type => 'ClientSite'

and seeing if the association lazy-loads correctly for that static type.

提笔落墨 2025-01-15 17:34:44

这修复了它(默认范围):

class Placement < ActiveRecord::Base
  belongs_to :client_destination_type

  default_scope :joins => :client_destination_type

  belongs_to :client_destination,
    :polymorphic => true,
    :foreign_type => 'client_destination_types.class_name'
end

this fixed it (default scope):

class Placement < ActiveRecord::Base
  belongs_to :client_destination_type

  default_scope :joins => :client_destination_type

  belongs_to :client_destination,
    :polymorphic => true,
    :foreign_type => 'client_destination_types.class_name'
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文