0

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...

1 Answer 1

2

More memory efficient way:

Form.last.items.pluck(:quantity)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.