# How do I validate using associated objects?

**URL:** <https://www.ruby-forum.com/t/how-do-i-validate-using-associated-objects/99844>\
**Category:** Rails\
**Created:** [July 25, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/how-do-i-validate-using-associated-objects/99844 "2006-07-25T22:13:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![eleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@eleo](https://www.ruby-forum.com/u/eleo)\
**Post date:** [July 25, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/how-do-i-validate-using-associated-objects/99844/1 "2006-07-25T22:13:41Z")

</div>

This doesn’t make any sense to me anymore. I have an Upload that  
belongs\_to a Comment. Within my Upload model I expect to be able to  
access the associated comment with comment.foo. I can only seem to do  
this AFTER validation. I’m assuming before\_create takes place between  
validation and updating the database, because when I try to access  
comment.foo within the validate method, I get “You have a nil object  
when you didn’t expect it! The error occured while evaluating  
nil.board”.

Now I am using single table inheritance, but I cannot access comment  
from the Upload model either. Strangely, I can access uploads from the  
Comment model.

This is frustrating the hell out of me ☹

class Comment \< ActiveRecord::Base  
belongs\_to :board  
has\_many :uploads  
end

class Upload \< ActiveRecord::Base  
belongs\_to :comment  
end

class Image \< Upload  
before\_create :create\_image  
before\_create :create\_thumbnail

def create\_image  
@image = Magick::ImageList.new.from\_blob(@upload\_blob)  
self[:image\_width] = @image.columns  
self[:image\_height] = @image.rows  
end

def create\_thumbnail  
@thumbnail = Magick::ImageList.new.from\_blob(@image[0].to\_blob)

```
if comment.parent_id == nil
  x = comment.board.setting(:parent_thumbnail_at_width)
  y = comment.board.setting(:parent_thumbnail_at_height)
else
  x = comment.board.setting(:reply_thumbnail_at_width)
  y = comment.board.setting(:reply_thumbnail_at_height)
end

if @thumbnail.columns > x or @thumbnail.rows > y
  @thumbnail.change_geometry!("#{x}x#{y}") {|cols, rows, img| 

```

img.resize!(cols, rows)}  
end  
end

def validate\_on\_create  
errors.add\_to\_base(comment.board.language(:error\_toobig)) if  
self[:image\_width] \> comment.board.setting(:maximum\_image\_width)  
end  
end

---

<div class="post-metadata">

**Author:** ![eleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@eleo](https://www.ruby-forum.com/u/eleo)\
**Post date:** [July 26, 2006, 1:19am UTC](https://www.ruby-forum.com/t/how-do-i-validate-using-associated-objects/99844/2 "2006-07-26T01:19:30Z")

</div>

Okay here’s an alternate question. Since I can access comment from  
before\_create, can I perform some kind of pseudo-validation there? ie  
is there any way to prevent the object from being saved from within the  
model outside of real validation methods?
