KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a model that uses a acts_as_nested_set fork, and I've added a method to the model to save the model and move the node into the set in one transaction. This method calls a validation method to make sure the move is valid, which returns true or false. If the validation fails, I want my save method to raise ActiveRecord::Rollback to rollback the transaction, but also return false to the caller. My model looks like this: class Category < ActiveRecord::Base acts_as_nested_set :dependent => :destroy, :scope => :journal def save_with_place_in_set(parent_id) Category.transaction do return false if !save_without_place_in_set if !validate_move parent_id raise ActiveRecord::Rollback and return false else place_in_nested_set parent_id return true end end end alias_method_chain :save, :place_in_set def validate_move(parent_id) # return true or false if the move is valid # ... end def place_in_nested_set(parent_id) # place the node in the correct place in the set # ... end end However, when I call save in a situation that would fail, the transaction is rolled back but the function returns nil : >> c = Category.new(:name => "test") => #<Category id: nil, name: "test" parent_id: nil, lft: nil, rgt: nil> >> c.save_with_place_in_set 47 => nil >> c.errors.full_messages => ["The specified parent is invalid"]
Tags (comma-separated)
Save Edits
Cancel