The following collection select helper for an element of ‘class ComponentOrder’ is failing with the error shown
<%= f.collection_select(:supplier_req_id, component.suppliers, :id, :price_per_um, prompt: t('component.assign_supplier') ) %>
undefined method `price_per_um' for an instance of Supplier
it runs when referencing its own variable f.collection_select(:supplier_req_id, component.suppliers, :id, :name_supplier,
using the collection of child records, as
class Component < ApplicationRecord
has_many :component_suppliers
[...]
class Supplier < ApplicationRecord
has_many :component_suppliers
[...]
class ComponentSupplier < ApplicationRecord
belongs_to :component
belongs_to :supplier
[...]
class ComponentOrder < ApplicationRecord
belongs_to :supplier_req, class_name: 'Supplier', optional: true
the join class defines a method to determine a value. The goal is to use it both as a method for storing data, as well as a view helper for the collection_select.
class ComponentSupplier < ApplicationRecord
def price_per_um
if self.last_quote_per_package
min_units = self.units_per_package * self.unit_quantity
value_per_um = self.last_quote_per_package / min_units
value_per_um.to_s + ' ' + unit_um.name
else
'?? ' + unit_um.name
end
end
self.units_per_package
and self.unit_quantity
have default values of 1
Rails raises the error because it is relying on the model definition of ComponentOrder.
How can the collection select therefore invoke the desired price_per_um
values?