Maybe I am overlooking something obvious but I’m trying to apply a practical scenario to this rails for Zombies tutorial.
——–Question————
Assuming the models and relationships are properly defined, find all the weapons that belong to Zombie ‘Ash’.
class Zombie < ActiveRecord::Base
has_many :weapons
end
class Weapon < ActiveRecord::Base
belongs_to :zombie
end
The tutorial accepts the answer of-
z = Zombie.find(1)
[#<Zombie id: 1, name: "Ash", graveyard: "Glen Haven Memorial Cemetery">]
z.weapons
[#<Weapon id: 1, name: "Hammer", strength: 1, zombie_id: 1>]
but for me that is not so practical, as if I were working on the project and that type of question came up I would do this.
ash = Zombie.where(:name => "Ash")
[#<Zombie id: 1, name: "Ash", graveyard: "Glen Haven Memorial Cemetery">]
But
ash.weapons
gives the output
#<NoMethodError: undefined method `weapons' for #<ActiveRecord::Relation:0x00000016334738>>
How can this be when the output for finding the zombie is exactly the same and the way I found the zombie is much more practical given the way the question is framed (even though they provide db tables). Could it be a nuance with the codeschool interactive console or am I missing something?
Thanks for your attention.
As your result shows itself:
This is some sort of beefed up array.
so if you would do:
will return all the weapons.