dimanche 11 septembre 2016

Ruby On Rails: how to return a collection (or array) of values from a query

I have a parent table called FORMS that has many ITEMS in ROR, and ITEMS belongs to FORMS. I need to grab the quantity values from ITEMS that belongs to the last entry in FORMS table.

form.rb

class Form < ApplicationRecord
  has_many :items
end

item.rb

class Item < ApplicationRecord
  belongs_to :form
end

Rails Console

2.3.1 :024 > Form.last.items.quantities
       Form Load (0.6ms)  SELECT  `forms`.* FROM `forms` ORDER BY `forms`.`id` DESC LIMIT 1
NoMethodError:   Item Load (0.3ms)  SELECT `items`.* FROM `items` WHERE `items`.`form_id` = 63
undefined method `quantities' for...

Aucun commentaire:

Enregistrer un commentaire