Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 283477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:22:37+00:00 2026-05-12T05:22:37+00:00

I have this code in my controller for a Rails app: def delete object

  • 0

I have this code in my controller for a Rails app:

  def delete
    object = model.datamapper_class.first(:sourced_id => params[:sourced_id])
    if object.blank?
      render :xml => "No #{resource} with sourced_id #{params[:sourced_id]}", :status => :not_found and return
    end
    object.destroy
    render :xml => "", :status => :no_content
  rescue MysqlError => e
    puts "raised MysqlError #{e.message}"
    render :xml => e.message, :status => :unprocessable_entity and return
  rescue Mysql::Error => e
    puts "raised Mysql::Error #{e.message}"
    render :xml => e.message, :status => :unprocessable_entity and return
  rescue Exception => e
    puts "not a MysqlError, instead it was a #{e.class.name}"
    render :xml => e.message, :status => :unprocessable_entity and return
  end

When I run my spec to make sure my foreign key constraints work, I get this:

not a MysqlError, instead it was a MysqlError

What could be going on here?


Some ancestor information: When I change the rescue to give me this:

puts MysqlError.ancestors
puts "****"
puts Mysql::Error.ancestors
puts "****"
puts e.class.ancestors

This is what I get:

Mysql::Error
StandardError
Exception
ActiveSupport::Dependencies::Blamable   ...
****
Mysql::Error
StandardError
Exception
ActiveSupport::Dependencies::Blamable   ...
****
MysqlError
StandardError
Exception
ActiveSupport::Dependencies::Blamable   ...

Could there be an alias in the global namespace that makes the MysqlError class unreachable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:22:37+00:00Added an answer on May 12, 2026 at 5:22 am

    This was a simple class redefinition bug. Ruby lets you redefine a top-level constant, but it doesn’t destroy the original constant when you do it. Objects that still hold references to that constant can still use it, so it could still be used to generate exceptions, like in the issue I was having.

    Since my redefinition was happening in dependencies, I solved this by searching for the original class in the Object space, and hanging on to a reference to it to use when catching exceptions. I added this line to my controller:

    ObjectSpace.each_object(Class){|k| @@mysql_error = k if k.name == 'MysqlError'}
    

    That gets a reference to the original version of MysqlError. Then I was able to do this:

      rescue @@mysql_error => e
        render :xml => e.message, :status => :unprocessable_entity and return
    

    This happens because the mysql gem is getting loaded after MysqlError has already been defined. Here is some test console joy:

    Loading test environment (Rails 2.3.2)
    >> MysqlError.object_id
    => 58446850
    >> require 'mysql'
    C:/Ruby/lib/ruby/gems/1.8/gems/mysql-2.7.3-x86-mswin32/ext/mysql.so: warning: already initialized constant MysqlError
    => true
    >> MysqlError.object_id
    => 58886080
    >> ObjectSpace._id2ref(MysqlError.object_id)
    => Mysql::Error
    

    You can do this in IRB without a require pretty easily; here’s a trick that works because irb doesn’t look up Hash by name every time you declare a Hash literal:

    irb(main):001:0> Hash = Class.new
    (irb):1: warning: already initialized constant Hash
    => Hash
    irb(main):002:0> hash = {:test => true}
    => {:test=>true}
    irb(main):003:0> hash.class
    => Hash
    irb(main):004:0> hash.is_a? Hash
    => false
    

    I can see why you might want to do this, it could be used like alias_method_chain for the global namespace. You could add a mutex to a class that isn’t threadsafe, for example, and not need to change old code to reference your threadsafe version. But I do wish RSpec hadn’t silenced that warning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in my controller and want to test this code line
I have this code in my controller: @cats = DirCat.all And this in a
So, I have this code in a controller: before_filter :require_login, :only => :new, :edit,
I have this controller in Code Igniter application. A value is initialized in the
I have this controller in Code Igniter that begins with class MyController extends CI_Controller
I have this simple code in my user_controller.rb file #listing all users def index
With ASP .NET MVC3. In my controller I have this portion of code MasterMindDnetEntities
In a Rails 3.1 app, I have a controller returning a set of objects
This is my kategori controller show action: def show @kategori = Kategori.find(params[:id]) @konkurrancer =
I've written a rails app that follows the regular directory structure (model code in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.