# Pulling ID from a just saved object?

**URL:** <https://www.ruby-forum.com/t/pulling-id-from-a-just-saved-object/118240>\
**Category:** Rails\
**Created:** [October 25, 2007, 1:56am UTC](https://www.ruby-forum.com/t/pulling-id-from-a-just-saved-object/118240 "2007-10-25T01:56:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [October 25, 2007, 1:56am UTC](https://www.ruby-forum.com/t/pulling-id-from-a-just-saved-object/118240/1 "2007-10-25T01:56:03Z")

</div>

Hi,

Can anyone tell me how I can pull a full url from an object I just  
saved?

I need to send the saved object’s id to my thanks\_url so I can build  
another object in the thanks\_url view.

if @video.save  
flash[:notice] = “”  
redirect\_to thanks\_url

```
else
  ....

```

end

Thanks!

-Mario

##FULL CODE

def new\_video  
@band = Band.find\_by\_id(params[:band\_id])  
return unless @band

```
# fix swfupload ampersand issue
params[:video_data] ||= params['amp;video_data']

# load data from form through swfupload
if params[:video_data]
  params[:video] ||= Marshal.load(params[:video_data]) #rescue {}
  # move swf's data to the proper key
  params[:video][:filename] = params[:Filedata]
end

@video = Video.new(params[:video])
@video.slug = URI.escape(@video.name)
@video.user_id = current_user.id
@video.band = @band

return unless request.post?

unless params[:Filename]
  if @video.valid?
    @temp = params[:video]
    @upload = true
    @old_params = {
      :video_data => Marshal.dump(params[:video])
    }
  end
  return
end

# Note: swfupload only cares about status code of the response, it

```

does  
# not follow redirections, you will have use it’s callbacks.  
if @video.save  
flash[:notice] = “”  
redirect\_to thanks\_url

```
else
raise @video.errors.inspect
  render and return false
end

```

end

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [October 25, 2007, 9:58am UTC](https://www.ruby-forum.com/t/pulling-id-from-a-just-saved-object/118240/2 "2007-10-25T09:58:01Z")

</div>

I realized I could just do this,

@video.save  
@video.id

My problem was that becuase of a data error, the video wasn’t saving in  
the first place.

Thanks,

-Mario

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [October 25, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/pulling-id-from-a-just-saved-object/118240/3 "2007-10-25T19:44:41Z")

</div>

On 10/25/07, Mario F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I realized I could just do this,
> 
> @video.save  
> @video.id
> 
> My problem was that becuase of a data error, the video wasn’t saving in  
> the first place.

You might want to consider using save! instead of save.

On a validation error save will return false, while save! will raise  
an exception.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
