# Default values in join model

**URL:** <https://www.ruby-forum.com/t/default-values-in-join-model/84657>\
**Category:** Rails\
**Created:** [February 6, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/default-values-in-join-model/84657 "2007-02-06T19:58:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 6, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/default-values-in-join-model/84657/1 "2007-02-06T19:58:56Z")

</div>

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.

Is there a clean way to do this?

Thanks,  
–Dean  
[http://blog.brewsession.com/](http://blog.brewsession.com/)

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 6, 2007, 9:38pm UTC](https://www.ruby-forum.com/t/default-values-in-join-model/84657/2 "2007-02-06T21:38:10Z")

</div>

On Feb 6, 1:58 pm, “Dean” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

Will this work?

class FermentableIngredient  
before\_save :copy\_from\_fermentable

protected  
def copy\_from\_fermentable  
if self.fermentable  
self.potential ||= fermentable.potential  
self.color ||= fermentable.color  
end  
end  
end

Dan M.

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 6, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/default-values-in-join-model/84657/3 "2007-02-06T22:18:17Z")

</div>

On Feb 6, 12:37 pm, “Dan M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> end
> 
> ```
> 
> end
> 
> Dan M.

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.

–Dean

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 7, 2007, 12:28am UTC](https://www.ruby-forum.com/t/default-values-in-join-model/84657/4 "2007-02-07T00:28:31Z")

</div>

On Feb 6, 4:17 pm, “Dean” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the suggestion, but I want to do it before saving.

Try this:

classFermentableIngredient \< AR::Base  
belongs\_to :recipe  
belongs\_to :fermentable

fermentable\_with\_copy=(f)  
self.potential ||= f.potential  
self.color ||= f.color  
fermentable\_without\_copy=f  
end  
alias\_method :fermentable\_without\_copy=, :fermentable=  
alias\_method :fermentable=, :fermentable\_with\_copy=  
end

Dan M.

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 7, 2007, 1:52am UTC](https://www.ruby-forum.com/t/default-values-in-join-model/84657/5 "2007-02-07T01:52:35Z")

</div>

> ```
> fermentable_without_copy=f
> 
> ```
> 
> end  
> alias\_method :fermentable\_without\_copy=, :fermentable=  
> alias\_method :fermentable=, :fermentable\_with\_copy=  
> end
> 
> Dan M.

That is _really_ close, except it does not assign the fermentable to  
the ingredient.

./script/console

> > i = FermentableIngredient.new  
> > =\> #\<FermentableIngredient…\>  
> > i.fermentable = Fermentable.find(1)  
> > =\> #\<Fermentable:… “potential”=\>“1.037”}\>  
> > i.potential  
> > =\> 1.037  
> > i.fermentable.potential  
> > NoMethodError: You have a nil object when you didn’t expect it!  
> > The error occurred while evaluating nil.potential  
> > from (irb):5

> > i.fermentable  
> > =\> nil

Looks like this should work from the ruby-doc on alias\_method  
[http://www.ruby-doc.org/core/classes/Module.html#M001706](http://www.ruby-doc.org/core/classes/Module.html#M001706)

alias\_method :orig\_fermentable=, :fermentable=  
def fermentable=(f)  
self.potential ||= f.potential  
self.color ||= f.color  
orig\_fermentable = f  
end

But it still does not assign the fermentable to the  
fermentable\_ingredient.

–Dean  
[http://blog.brewsession.com/](http://blog.brewsession.com/)

On Feb 6, 3:27 pm, “Dan M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 7, 2007, 1:56am UTC](https://www.ruby-forum.com/t/default-values-in-join-model/84657/6 "2007-02-07T01:56:36Z")

</div>

And the answer is:

alias\_method :orig\_fermentable=, :fermentable=

def fermentable=(f)  
self.potential ||= f.potential  
self.color ||= f.color  
self.orig\_fermentable = f  
end

Thanks to Dan for the help

–Dean  
[http://blog.brewsession.com/](http://blog.brewsession.com/)
