How to Access a Callback Return Value

I have an Image model that is supported by a Binary model. The latter
contains metadata about a physical file and performs all operations
related to the physical file (I will eventually have other models for
different file types). I have a binary.upload method working, but I’d
like to move its call from my ImagesController into the
image.before_create callback. The binary.upload method returns the
binary object. Since my Image model belongs_to my Binary model, I need
the binary.id value to save the Image. How can I access that?

In my ImagesController, I currently have:

def create
@image = Image.new( params[:image] )
# @binary = Binary.new

# if @binary.upload( params[:image][:upload] )
#  @image.binary_id = @binary.id

  if @image.save
    flash[:notice] = "Successfully created image."
    redirect_to @image
  else
    render :action => 'new'
  end
end

end

I’ve commented the code that I’d like to move into the Image model
callback, but I’m not sure how the mechanics would work. Can someone
point me in the right direction?

Any help would be much appreciated. I’m still learning how to access
and manipulate objects in Ruby, as you can see.

On Aug 11, 10:10 pm, Rob W. [email protected] wrote:

I have an Image model that is supported by a Binary model. The latter
contains metadata about a physical file and performs all operations
related to the physical file (I will eventually have other models for
different file types). I have a binary.upload method working, but I’d
like to move its call from my ImagesController into the
image.before_create callback. The binary.upload method returns the
binary object. Since my Image model belongs_to my Binary model, I need
the binary.id value to save the Image. How can I access that?

why not do the assignment from the before_create callback ? I’m not
entirely sure why you’re trying to do this .

Fred

On Aug 11, 5:53 pm, Frederick C. [email protected]
wrote:

why not do the assignment from the before_create callback ? I’m not
entirely sure why you’re trying to do this .

I guess what I’m saying is that I’m not sure how to do this. Here’s
what I’m thinking that I’d like to do in terms of order of operations:

ImagesController::create

  • accepts the form input. This includes the uploaded file since the
    Image model has an accessor attribute (:upload).
  • instantiates a new image with the passed params.
  • calls the Image model’s save method

Image

  • calls the Binary model to upload the physical file and set/store
    its properties
  • sets its own binary_id property based on the return value from
    the call to Binary::upload
  • saves itself

Maybe there’s a more efficient way to do this than how I’m thinking?
If not, I guess what I’m not sure about is how to make that all work
together most efficiently. There are several different callback
mechanisms in Rails and I don’t have a good enough feel for which is
most appropriate in this scenario. I’m thinking of something like this
in the Image model:

class Image < ActiveRecord::Base
belongs_to :binary

validates_presence_of( :upload )

before_create :upload_physical_file

attr_accessor :upload

protected

def upload_physical_file
@binary = self.binary.upload( :upload )
self.binary_id = @binary.id
end
end

I have an unexpected nil object that I’ll debug, but I don’t have a
good feel that I’m doing this the best way. Being completely new to
Ruby and Rails, I don’t have a good feel for how to access related
models or what objects are available at which times. For example, I
don’t know whether I have to explicitly instantiate a Binary object in
my Image model or, since they’re related, I can assume one is
instantiated and access it as self.binary.

At this point, I’m fighting the learning curve and may not even be
asking the right questions. Any advice you can offer based on either
the scenario or the posted code would be much appreciated.

Thanks again.

On Aug 11, 11:43 pm, Rob W. [email protected] wrote:

I have an unexpected nil object that I’ll debug, but I don’t have a
good feel that I’m doing this the best way. Being completely new to
Ruby and Rails, I don’t have a good feel for how to access related
models or what objects are available at which times. For example, I
don’t know whether I have to explicitly instantiate a Binary object in
my Image model or, since they’re related, I can assume one is
instantiated and access it as self.binary.

You would have to create an instance of Binary. associations just add
a bunch of methods that make manipulation objects easier.
if you do build_binary that will both create an instance of binary and
associate it with the image (ie you don’t have have to set
self.binary_id).

You may want to read the guide about associations at

Fred