# Question about Rails behaviour

**URL:** <https://www.ruby-forum.com/t/question-about-rails-behaviour/151868>\
**Category:** Rails\
**Created:** [October 27, 2008, 8:15am UTC](https://www.ruby-forum.com/t/question-about-rails-behaviour/151868 "2008-10-27T08:15:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [October 27, 2008, 8:15am UTC](https://www.ruby-forum.com/t/question-about-rails-behaviour/151868/1 "2008-10-27T08:15:47Z")

</div>

Hello,

Well I have a post and a post\_picture model:

class Post \< ActiveRecord::Base  
has\_one :post\_picture  
end

class PostPicture \< ActiveRecord::Base  
belongs\_to :post  
end

When a user creates a post he/she can upload a picture. So in the  
create action on the posts\_controller I have the following:

def create  
@post = Post.new(params[:post])  
@post\_picture = PostPicture.new(:uploaded\_data =\>  
params[:post\_picture\_file])  
@post.post\_picture = @post\_picture  
if @post.save  
…  
end

As you can see I relate the @post\_picture with the @post using the  
line of code

“@post.post\_picture = @post\_picture”

My question is:

When I do the @post.save does rails treats this as a transaction? (I  
want to know because there is maybe possible data loss if the post is  
saved into the posts table and the post\_picture is not saved into the  
post\_pictures table or viceversa.

Thanks,

Elioncho

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [October 27, 2008, 9:15am UTC](https://www.ruby-forum.com/t/question-about-rails-behaviour/151868/2 "2008-10-27T09:15:31Z")

</div>

since post\_picture should await post\_id as a foreign key, post\_picture  
will not be saved properly (or at least will not relate to the  
specified post) if post is not saved. as long as there aint no post.id  
(which is created while saving) you can’t save the relation between  
these two objects.

i’d say it’s best to save @post and after that create @post\_picture  
and save it as well.

def create  
@post = Post.new(params[:post])  
if @post.save  
@post\_picture = PostPicture.new(:uploaded\_data =\>  
params[:post\_picture\_file])  
@post.post\_picture = @post\_picture  
if @post\_picture.save  
# post and post\_picture are saved correctly  
# …  
else  
# post\_picture could not be saved (=\> error handling)  
end  
else  
# post could not be saved (=\> error handling)  
end  
end

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [October 27, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/question-about-rails-behaviour/151868/3 "2008-10-27T18:45:19Z")

</div>

Thanks for your reply. It was helpful.

Elioncho
