I have a has_many :through and I would like to take values from one
side and put them in the join model. Perhaps some code would be more
clear.
class Recipe < AR::Base
has_many :fermentable_ingredients
has_many :fermentables, :through => :fermentable_ingredients
end
class Fermentable < AR::Base
has_many :fermentable_ingredients
has_many :recipes, :through => :fermentable_ingredients
end
classFermentableIngredient < AR::Base
belongs_to :recipe
belongs_to :fermentable
end
Simple enough. When I assign a fermentable to a
fermentable_ingredienet, I would like to populate the
fermentable_ingredient with values in fermentable. Something like
this:
def fermentable=(f)
self.potential = f.potential if potential.nil?
self.color = f.color if color.nil?
super
end
I know this doesn't work because the call to super fails.
Thanks for the suggestion, but I want to do it before saving. Like
here:
class FermentableIngredientsController < ApplicationController
def new
@fermentable_ingredient = FermentableIngredient.new
@fermentable_ingredient.fermentable =
Fermentable.find( params[:fermentable_id] )
render
end
end
That way the form fields for @fermentable_ingredient will be
automatically filled out. As it is I have to call an extra method
before render to transfer the values.