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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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
On 27 Okt., 08:14, elioncho
<elion...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Thanks for your reply. It was helpful. Elioncho On Oct 27, 3:14 am, none <mayer.domi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On 27 Okt., 08:14,elioncho<elion...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---