渴望加载不在Rails 2.2.2中工作

时间:2022-10-05 14:30:47

I'm working with models analogous to the following:

我正在使用类似于以下的模型:

class Owner < ActiveRecord::Base
  has_many :owned
end

class Owned < ActiveRecord::Base
  belongs_to :owner
end

You can presume that owned_id and owner_id are in the right places. The trouble is that, in a controller for a different mvc chain in the app,

您可以假设owned_id和owner_id位于正确的位置。麻烦的是,在应用程序中用于不同mvc链的控制器中,

@owner = Owned.find_by_id(owned_id, :include => :owner)

doesn't work. I get the owner_id, column, naturally, but can't then do

不起作用。我自然得到owner_id,列,但不能这样做

@owned.owner # is just nil

What gives? I mean, I could do the assignment directly before passing the result on to the view:

是什么赋予了?我的意思是,我可以在将结果传递给视图之前直接进行分配:

@owned.owner = Owner.find_by_id(@owned.owner_id)

but that just seems silly. Come on, embarrass me. What's the obvious thing that I've missed? This works in other places in my app, but I can't spot the differences. Are there some common traps? Anything helps.

但这看起来很傻。来吧,让我难堪。我错过了哪些显而易见的事情?这适用于我的应用程序的其他地方,但我无法发现差异。有一些常见的陷阱吗?一切都有帮助。

Thank you

2 个解决方案

#1


I just keep winning. The corresponding 'Owner' object had been deleted from the owners table.

我一直在赢。相应的“所有者”对象已从所有者表中删除。

The funny thing is, before I created an account, I had tons of karma on my cookie-based identity. Then my cookies became corrupted, and I can't ask anything but stupid questions anymore, and my karma sits at 1. Oh well.

有趣的是,在我创建一个帐户之前,我在基于cookie的身份上有很多业力。然后我的饼干变得腐败了,除了愚蠢的问题我什么都不能问,而我的业力就在1.哦。

#2


Reputation on * is not cookie based. You may have to log in again or something.

*上的信誉不是基于cookie的。您可能需要再次登录或其他内容。

Your question seems to imply that you have an owned_id field in the owner table. You don't need that and should remove it.

您的问题似乎暗示您在所有者表中有一个owned_id字段。你不需要它,应该删除它。

You just need an owner_id integer field in the owned table.

您只需要拥有的表中的owner_id整数字段。

You can access your records and relationships in a number of ways. First let's start by accessing the owner record first.

您可以通过多种方式访问​​您的记录和关系。首先让我们首先访问所有者记录。

owner = Owner.find(owner_id)
owned = owner.owned # this is an array since you a 'has_many' relationship

Normally you'd want to access the owned records in the following way:

通常,您希望以下列方式访问拥有的记录:

for owned in owner.owned
 puts owned.name # or access any other attributes
end

If you would like to access the owned records first you could do the following:

如果您想首先访问拥有的记录,您可以执行以下操作:

@owned = Owned.find(:all, :conditions => [ "owner_id = ?", owner_id ])
# @owned is an array so you need to iterate through it
for owned in @owned
  puts owned.owner.name # or access any other attribute from the owner
end

Once you've got these queries working fine you can worry about eager loading by adding :include in your find statements. Note that this can be of interest for optimization but not necessary from the get go.

一旦你使这些查询正常工作,你可以通过在find语句中添加:include来担心急切加载。请注意,这可能对优化感兴趣,但从一开始就没有必要。

I hope this helps.

我希望这有帮助。

#1


I just keep winning. The corresponding 'Owner' object had been deleted from the owners table.

我一直在赢。相应的“所有者”对象已从所有者表中删除。

The funny thing is, before I created an account, I had tons of karma on my cookie-based identity. Then my cookies became corrupted, and I can't ask anything but stupid questions anymore, and my karma sits at 1. Oh well.

有趣的是,在我创建一个帐户之前,我在基于cookie的身份上有很多业力。然后我的饼干变得腐败了,除了愚蠢的问题我什么都不能问,而我的业力就在1.哦。

#2


Reputation on * is not cookie based. You may have to log in again or something.

*上的信誉不是基于cookie的。您可能需要再次登录或其他内容。

Your question seems to imply that you have an owned_id field in the owner table. You don't need that and should remove it.

您的问题似乎暗示您在所有者表中有一个owned_id字段。你不需要它,应该删除它。

You just need an owner_id integer field in the owned table.

您只需要拥有的表中的owner_id整数字段。

You can access your records and relationships in a number of ways. First let's start by accessing the owner record first.

您可以通过多种方式访问​​您的记录和关系。首先让我们首先访问所有者记录。

owner = Owner.find(owner_id)
owned = owner.owned # this is an array since you a 'has_many' relationship

Normally you'd want to access the owned records in the following way:

通常,您希望以下列方式访问拥有的记录:

for owned in owner.owned
 puts owned.name # or access any other attributes
end

If you would like to access the owned records first you could do the following:

如果您想首先访问拥有的记录,您可以执行以下操作:

@owned = Owned.find(:all, :conditions => [ "owner_id = ?", owner_id ])
# @owned is an array so you need to iterate through it
for owned in @owned
  puts owned.owner.name # or access any other attribute from the owner
end

Once you've got these queries working fine you can worry about eager loading by adding :include in your find statements. Note that this can be of interest for optimization but not necessary from the get go.

一旦你使这些查询正常工作,你可以通过在find语句中添加:include来担心急切加载。请注意,这可能对优化感兴趣,但从一开始就没有必要。

I hope this helps.

我希望这有帮助。