Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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
Database 发现误会_Database_Ruby On Rails 3_Methods - Fatal编程技术网

Database 发现误会

Database 发现误会,database,ruby-on-rails-3,methods,Database,Ruby On Rails 3,Methods,也许我忽略了一些显而易见的东西,但我正在尝试将一个实际的场景应用到这个railsforzombies教程中 --------问题:------------ 假设模型和关系被正确定义,找到所有属于僵尸“灰烬”的武器 class Zombie < ActiveRecord::Base has_many :weapons end class Weapon < ActiveRecord::Base belongs_to :zombie end 给出输出 #<NoMetho­

也许我忽略了一些显而易见的东西,但我正在尝试将一个实际的场景应用到这个railsforzombies教程中

--------问题:------------ 假设模型和关系被正确定义,找到所有属于僵尸“灰烬”的武器

class Zombie < ActiveRecord::Base
  has_many :weapons
end

class Weapon < ActiveRecord::Base
  belongs_to :zombie
end
给出输出

#<NoMetho­dError: un­defined me­thod `weap­ons' for #­<ActiveR­ecord::Rel­ation:0x00­0000163347­38>>

#正如您的结果所示:

[#<Zombie ­id: 1, nam­e: "Ash", ­graveyard:­ "Glen Hav­en Memoria­l Cemetery­">]

将返回所有武器。

我仍然不确定是否理解,因为两个阵列都相同地“增强”,但其中一个阵列对武器调用的响应不同。我想我更大的问题是,我是否可以/是否会在实践中使用我认为是实用的调用?当您找到(1)时,它将返回单个资源。其中将返回活动记录关系。如果你在这两种情况下都使用ash.class,你会发现它们是两种不同的东西
ash.weapons
#<NoMetho­dError: un­defined me­thod `weap­ons' for #­<ActiveR­ecord::Rel­ation:0x00­0000163347­38>>
[#<Zombie ­id: 1, nam­e: "Ash", ­graveyard:­ "Glen Hav­en Memoria­l Cemetery­">]
ash = Zombie.where(:name => "Ash").first
ash.weapons