Skip to content

Instantly share code, notes, and snippets.

# Wrong!
# If foo is valid but bar is not, foo is still saved.
# This breaks the transactional nature
#
def create
Foo.transaction do
@foo = Foo.create(params[:foo])
@bar = Bar.create(params[:bar])
if @foo.save and @bar.save
redirect_to some_url
for i in $(find db/migrate |grep mondrian |cut -d '/' -f 3 | cut -d '_' -f 1 )
do
rake db:migrate:down VERSION=$i
rake db:migrate:up VERSION=$i
done
rake mondrian:init
class Device
validates_uniqueness_of :device_number, :scope => :customer_id
after_create: assign_device_number
def assign_device_number
begin
device_number = self.customer.devices.map(&:device_number).compact.max) || 0 + 1
save!
rescue Whatever gets thrown for validation error
class Device
# ...
def friendly_name
"#{self.customer.device_name_prefix} #{self.device_number}"
end
end