I have bellow one model has many other model.

The question is when i update 2 models. One model is validate failed.
The other model can not rollback.

Sample:

class User < ActiveRecord::Base
has_many :products, :dependent => :destroy
validates_presence_of :name
end

class Product < ActiveRecord::Base
belongs_to :user
end

# do a update

User.transaction do
user = User.find(1)
p user.products # => [#product1,#product2]
user.products = [#product3, #product4]
user.name = ""
user.save
end

=>
validate error: Name can't be blank

But when you run bellow.
user = User.find(1)
p user.products # => [#product3,#product4]

How can i resolve this problem?
I don't need products are updated.

I use
.mysql innoDB
.Rails 3.0.3
.activerecord (3.0.3)


--
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@googlegroups.com.
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.

Search Discussions

  • Frederick Cheung at Dec 15, 2011 at 12:22 pm

    On Dec 15, 9:04 am, Grant wrote:
    User.transaction do
    user = User.find(1)
    p user.products # => [#product1,#product2]
    user.products = [#product3, #product4]
    user.name = ""
    user.save
    end

    =>
    validate error: Name can't be blank

    But when you run bellow.
    user = User.find(1)
    p user.products   # => [#product3,#product4]

    How can i resolve this problem?
    I don't need products are updated.

    The save is wrapped in a save point which should be rolled back,
    however when you assign to a has_many like that the change is made
    straightaway to the products.
    You could raise ActiveRecord::Rollback to force the outermost
    transaction to be rolled back

    Fred

    --
    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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 15, '11 at 12:19p
activeDec 15, '11 at 12:22p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Grant: 1 post Frederick Cheung: 1 post

People

Translate

site design / logo © 2021 Grokbase