Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RoR:fetch通过另一个有很多关联_Ruby On Rails - Fatal编程技术网

Ruby on rails RoR:fetch通过另一个有很多关联

Ruby on rails RoR:fetch通过另一个有很多关联,ruby-on-rails,Ruby On Rails,我遵循官方文件:第4.3.3.4节 我有以下型号: class Nomination < ActiveRecord::Base belongs_to :festival has_many :festival_histories, -> { includes :awards } attr_accessible :name end class FestivalHistory < ActiveRecord::Base has_many :awards belon

我遵循官方文件:第4.3.3.4节

我有以下型号:

class Nomination < ActiveRecord::Base
  belongs_to :festival
  has_many :festival_histories, -> { includes :awards }
  attr_accessible :name
end

class FestivalHistory < ActiveRecord::Base
  has_many :awards
  belongs_to :nomination
  belongs_to :festival
end

class Award < ActiveRecord::Base
  belongs_to :festival_history
  belongs_to :case, inverse_of: :awards
  has_attached_file :image
  attr_accessible :name, :festival_history_id, :image
end
我明白了

NoMethodError: undefined method `awards' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_FestivalHistory:0x000001019cd400>
NoMethodError:未定义的方法“奖励”#

我已经重新加载了控制台,所以问题还没有确定…

以下是您的控制台出现的问题,因为festival_histories是一个记录集合,您无法获得一个集合的
奖励,只能获得一个单独的记录。所以不是

class Nomination < ActiveRecord::Base
  belongs_to :festival
  has_many :festival_histories, -> { includes :awards }
  has_many :awards, through: :festival_histories
  attr_accessible :name
end
n = Nomination.first
n.festival_histories.awards
你需要

n = Nomination.first
n.festival_histories.each { |r| puts r.awards}
查看每个
音乐节的奖项\u历史


(是的,你是如何
包括:
:延迟加载的奖励是有效的,文档中没有错误;)

文档没有问题:)

正如JTG所说,你不可能获得所有节日历史的奖项,只能获得特定历史的奖项

区别在于:

带有
包括
选项:

n = Nomination.first
Nomination Load (0.4ms) SELECT "nominations".* FROM "nominations" ORDER BY "nominations"."id" ASC LIMIT 1

n.festival_histories
FestivalHistory Load (25.5ms)  SELECT "festival_histories".* FROM "festival_histories" WHERE "festival_histories"."nomination_id" = ?  [["nomination_id", 1]]
Award Load (0.7ms)  SELECT "awards".* FROM "awards" WHERE "awards"."festival_history_id" IN (1)

n.festival_histories.first.awards
NO QUERY!
n = Nomination.first
Nomination Load (0.4ms) SELECT "nominations".* FROM "nominations" ORDER BY "nominations"."id" ASC LIMIT 1

n.festival_histories
FestivalHistory Load (25.5ms)  SELECT "festival_histories".* FROM "festival_histories" WHERE "festival_histories"."nomination_id" = ?  [["nomination_id", 1]]

n.festival_histories.first.awards
Award Load (0.7ms)  SELECT "awards".* FROM "awards" WHERE "awards"."festival_history_id" = ?  [["festival_history_id", 1]]
不带
包括
选项:

n = Nomination.first
Nomination Load (0.4ms) SELECT "nominations".* FROM "nominations" ORDER BY "nominations"."id" ASC LIMIT 1

n.festival_histories
FestivalHistory Load (25.5ms)  SELECT "festival_histories".* FROM "festival_histories" WHERE "festival_histories"."nomination_id" = ?  [["nomination_id", 1]]
Award Load (0.7ms)  SELECT "awards".* FROM "awards" WHERE "awards"."festival_history_id" IN (1)

n.festival_histories.first.awards
NO QUERY!
n = Nomination.first
Nomination Load (0.4ms) SELECT "nominations".* FROM "nominations" ORDER BY "nominations"."id" ASC LIMIT 1

n.festival_histories
FestivalHistory Load (25.5ms)  SELECT "festival_histories".* FROM "festival_histories" WHERE "festival_histories"."nomination_id" = ?  [["nomination_id", 1]]

n.festival_histories.first.awards
Award Load (0.7ms)  SELECT "awards".* FROM "awards" WHERE "awards"."festival_history_id" = ?  [["festival_history_id", 1]]

我认为现在的区别是显而易见的:)

是的,我知道这一点,但我正试图弄清楚——这是文档中的错误吗?可能是。我认为这可能是一种试图在不使用太多代码的情况下理解这个想法的尝试。例如,他们可以显示一个代码块,其中包含一个循环,循环遍历客户的每个订单并显示其行项目。也许像@customer->orders->line\u items这样的东西更有意义?