# Accessing associated models in a validate method

**URL:** <https://www.ruby-forum.com/t/accessing-associated-models-in-a-validate-method/65155>\
**Category:** Rails\
**Created:** [February 9, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/accessing-associated-models-in-a-validate-method/65155 "2006-02-09T20:57:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eddie\_Hillenbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/65b543/32.png) [@Eddie\_Hillenbrand](https://www.ruby-forum.com/u/Eddie_Hillenbrand)\
**Post date:** [February 9, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/accessing-associated-models-in-a-validate-method/65155/1 "2006-02-09T20:57:41Z")

</div>

Why can’t I access an associated model in a validate method? I have  
some code that looks like the following:

Models:  
class Product \< ActiveRecord::Base  
has\_many: upcs  
end

class Upc \< ActiveRecord::Base  
belongs\_to :product  
def validate  
errors.add\_on\_blank(‘upc’) unless product.new\_record?  
end  
end

Controller:  
def create  
@product = Product.new(params[:product])  
@upc = Upc.new(params[:upc])  
@product.upcs \<\< @upc unless @upc.upc.empty?

if @product.save  
# …  
end  
end

When the create method gets called I get the NoMethodError “The error  
occured while evaluating nil.new\_record?” Why is product nil? How  
can I access associated models in a validate method.

Thanks.

---

<div class="post-metadata">

**Author:** ![Eddie\_Hillenbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/65b543/32.png) [@Eddie\_Hillenbrand](https://www.ruby-forum.com/u/Eddie_Hillenbrand)\
**Post date:** [February 10, 2006, 11:39am UTC](https://www.ruby-forum.com/t/accessing-associated-models-in-a-validate-method/65155/2 "2006-02-10T11:39:03Z")

</div>

Eddie Hillenbrand wrote:

> def validate  
> if @product.save  
> # …  
> end  
> end
> 
> When the create method gets called I get the NoMethodError “The error  
> occured while evaluating nil.new\_record?” Why is product nil? How can  
> I access associated models in a validate method.

The “unless @upc.upc.empty?” code will stop @upc.product being set to  
@product if upc is the empty string, so validate can’t see it. Why  
don’t you remove this condition and add a “validates\_associated :upcs”  
line to Product, so that @product and its new upcs’s will only save if  
everything’s valid.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Eddie\_Hillenbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/65b543/32.png) [@Eddie\_Hillenbrand](https://www.ruby-forum.com/u/Eddie_Hillenbrand)\
**Post date:** [February 12, 2006, 1:37am UTC](https://www.ruby-forum.com/t/accessing-associated-models-in-a-validate-method/65155/3 "2006-02-12T01:37:40Z")

</div>

Eddie Hillenbrand wrote:

> Even with “unless @upc.upc.empty?” commented out “product” is still nil  
> in the validate method.
> 
> The requirements for the app are such that a product can be created  
> without a UPC, but once a product is saved a blank UPC can’t be added.  
> If you know a way to do this with the validation class methods, please  
> elaborate, I’d really like a better way to do this.

Is it that a product’s UPC can start off blank, but once it’s  
set can’t return to being blank; or is it that the UPC must be  
set no later than the first save after creation?

Also, try changing “product.new\_record?” to “self.product.new\_record?”.  
That sometimes fixes these problems for reasons I can’t yet fathom.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Eddie\_Hillenbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/65b543/32.png) [@Eddie\_Hillenbrand](https://www.ruby-forum.com/u/Eddie_Hillenbrand)\
**Post date:** [February 10, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/accessing-associated-models-in-a-validate-method/65155/4 "2006-02-10T16:48:02Z")

</div>

On Feb 10, 2006, at 3:37 AM, Mark Reginald J. wrote:

> The “unless @upc.upc.empty?” code will stop @upc.product being set to  
> @product if upc is the empty string, so validate can’t see it. Why  
> don’t you remove this condition and add a “validates\_associated :upcs”  
> line to Product, so that @product and its new upcs’s will only save if  
> everything’s valid.

Even with “unless @upc.upc.empty?” commented out “product” is still  
nil in the validate method.

The requirements for the app are such that a product can be created  
without a UPC, but once a product is saved a blank UPC can’t be  
added. If you know a way to do this with the validation class  
methods, please elaborate, I’d really like a better way to do this.
