How do I validate using associated objects?

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 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 within the validate method, I get “You have a nil object
when you didn’t expect it! The error occured while evaluating

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

class Comment < ActiveRecord::Base
belongs_to :board
has_many :uploads

class Upload < ActiveRecord::Base
belongs_to :comment

class Image < Upload
before_create :create_image
before_create :create_thumbnail

def create_image
@image =
self[:image_width] = @image.columns
self[:image_height] = @image.rows

def create_thumbnail
@thumbnail =[0].to_blob)

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

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

img.resize!(cols, rows)}

def validate_on_create
errors.add_to_base(comment.board.language(:error_toobig)) if
self[:image_width] > comment.board.setting(:maximum_image_width)

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs