# How to Access a Callback Return Value

**URL:** <https://www.ruby-forum.com/t/how-to-access-a-callback-return-value/173609>\
**Category:** Rails\
**Created:** [August 11, 2009, 11:12pm UTC](https://www.ruby-forum.com/t/how-to-access-a-callback-return-value/173609 "2009-08-11T23:12:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwhiten1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@rwhiten1](https://www.ruby-forum.com/u/rwhiten1)\
**Post date:** [August 11, 2009, 11:12pm UTC](https://www.ruby-forum.com/t/how-to-access-a-callback-return-value/173609/1 "2009-08-11T23:12:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rwhiten1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@rwhiten1](https://www.ruby-forum.com/u/rwhiten1)\
**Post date:** [August 11, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/how-to-access-a-callback-return-value/173609/2 "2009-08-11T23:54:52Z")

</div>

On Aug 11, 10:10 pm, Rob W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![rwhiten1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@rwhiten1](https://www.ruby-forum.com/u/rwhiten1)\
**Post date:** [August 12, 2009, 12:44am UTC](https://www.ruby-forum.com/t/how-to-access-a-callback-return-value/173609/3 "2009-08-12T00:44:37Z")

</div>

On Aug 11, 5:53 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![rwhiten1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@rwhiten1](https://www.ruby-forum.com/u/rwhiten1)\
**Post date:** [August 12, 2009, 2:59am UTC](https://www.ruby-forum.com/t/how-to-access-a-callback-return-value/173609/4 "2009-08-12T02:59:13Z")

</div>

On Aug 11, 11:43 pm, Rob W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

> **[Ruby on Rails Guides](https://guides.rubyonrails.org)**
>
> Ruby on Rails Guides

Fred
