SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RoR: strange error, please help

    I'm just learning RoR so bear with me.

    I have two models, AgBoard and BoardAttribute, with declarations like so:


    class AgBoard < ActiveRecord::Base

    acts_as_tree rder => "title"
    has_one :BoardAttribute, :dependent => true


    class BoardAttribute < ActiveRecord::Base
    set_table_name "ag_board_attributes"

    belongs_to :AgBoard




    I'm using the standard scaffolding to get this to work, the destroy method is as follows:

    def destroy
    board = AgBoard.find(params[:id]).destroy
    redirect_to :action => 'list'
    end


    If I don't include the :dependent => true option in the has_one relationship I get foreign key constraint errors because it's not deleting the child row first. If I do add it, I get the following error: wrong number of arguments (0 for 1)


    I'm pretty ignorant when it comes to rails so I have no idea where to begin looking. Ultimately, all I want is for it to delete the child row correctly when I delete a parent row.

    Any help/suggestions would be greatly appreciated.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you give some more information about the error: file/line/function etc. And the code that triggered the error.

  3. #3
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have that same error

  4. #4
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error occurs when this line is executed:
    board = AgBoard.find(params[:id]).destroy

    The following is the full trace.
    Code:
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:335:in `destroy'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:335:in `callback'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:335:in `callback'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:330:in `callback'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:320:in `destroy_without_transactions'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:122:in `destroy'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in `transaction'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:91:in `transaction'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:118:in `transaction'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:122:in `destroy'
    #{RAILS_ROOT}/app/controllers/admin_forum_controller.rb:52:in `destroy'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in `perform_action_without_filters'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in `perform_action_without_benchmark'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in `perform_action'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in `process_without_session_management_support'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:in `process'
    /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in `dispatch'
    /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in `handle_dispatch'
    /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in `service'
    /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
    /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
    /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
    /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
    /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
    /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in `dispatch'
    /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59
    /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
    /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in `require'
    /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28
    /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
    /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in `require'
    #{RAILS_ROOT}/script/server:3

    Thanks for the help, I really appreciate it. I've been banging my head against this error for a day or two now. If there's anything else you need just let me know.

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you get the same error if you do:
    Code:
    AgBoard.find(params[:id]).destroy
    instead of:
    Code:
    board = AgBoard.find(params[:id]).destroy
    Rails has two tool for such things: breakpointer and console. You should try your console first:
    Code:
    ruby script/console
    And try your code there.

    If that doesn't help you can use breakpointer: http://wiki.rubyonrails.com/rails/pa...WithBreakpoint

  6. #6
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response. The changes have no effect, I only split it up like that trying to see if I could get it to make a difference.

    Thanks for the advice on the tools though, I didn't know either existed. Agile Web Development with Rails hardly mentions the console and I apparently missed what it did show. I'm definately much obliged.

    I've been looking through the code(which I've posted below) trying to see what's going on and I'm finding it tough to follow, mostly because of my lack of experience with Ruby. I'm hoping someone can explain a few things to me.

    I'm trying to trace the error, and it starts with the method destroy_with_transactions, which turns around and calls transaction with the method destroy_without_transactions(both of which are aliases). The thing is, there's two definitions of transaction, but with identical method signatures. I don't understand the lookup rules well enough to know which is being called, and/or why. I have Programming Ruby the pragmatic programmers guide, and I can't really find anything on it. I've also googled around and I'm still having trouble. If there's any documentation online about Ruby's lookup rules I would *really* appreciate it.

    They say the best way to learn something is to dig in Hopefully by the time this is over with I'll have a *much* more thorough understanding of Ruby/Rails.


    The following is code from ActiveRecord 1.13.2 from transaction.rb minus the comments


    Code:
    module ActiveRecord
      module Transactions # :nodoc:
        TRANSACTION_MUTEX = Mutex.new
    
        class TransactionError < ActiveRecordError # :nodoc:
        end
    
        def self.append_features(base)
          super
          base.extend(ClassMethods)
    
          base.class_eval do
            alias_method :destroy_without_transactions, :destroy
            alias_method :destroy, :destroy_with_transactions
    
            alias_method :save_without_transactions, :save
            alias_method :save, :save_with_transactions
          end
        end
    
        module ClassMethods
          def transaction(*objects, &block)
            previous_handler = trap('TERM') { raise TransactionError, "Transaction aborted" }
            lock_mutex
            
            begin
              objects.each { |o| o.extend(Transaction::Simple) }
              objects.each { |o| o.start_transaction }
    
              result = connection.transaction(Thread.current['start_db_transaction'], &block)
    
              objects.each { |o| o.commit_transaction }
              return result
            rescue Exception => object_transaction_rollback
              objects.each { |o| o.abort_transaction }
              raise
            ensure
              unlock_mutex
              trap('TERM', previous_handler)
            end
          end
          
          def lock_mutex#:nodoc:
            Thread.current['open_transactions'] ||= 0
            TRANSACTION_MUTEX.lock if Thread.current['open_transactions'] == 0
            Thread.current['start_db_transaction'] = (Thread.current['open_transactions'] == 0)
            Thread.current['open_transactions'] += 1
          end
          
          def unlock_mutex#:nodoc:
            Thread.current['open_transactions'] -= 1
            TRANSACTION_MUTEX.unlock if Thread.current['open_transactions'] == 0
          end
        end
    
        def transaction(*objects, &block)
          self.class.transaction(*objects, &block)
        end
    
        def destroy_with_transactions #:nodoc:
          transaction { destroy_without_transactions }
        end
        
        def save_with_transactions(perform_validation = true) #:nodoc:
          transaction { save_without_transactions(perform_validation) }
        end
      end
    end

  7. #7
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its far more likely that the error is caused by your own code...what is the line of code in your controller that is failling and what is in your logs?

  8. #8
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Luke, my goal in delving into ActiveRecord wasn't to find a bug with ActiveRecord, it was to trace the call that was failing and see why it was failing. I have no doubt that it's something I'm doing, but as far as I can tell it *is* failing in Activerecord somewhere.

    Code:
    AgBoard.find(params[:id]).destroy
    I've split out the AgBoard.find and the destroy call to test, and it's definately the destroy call that's failing. I'm trying to determine why.

    As to the logs, I looked through them and I don't recall anything that jumped out at me, but that was before I upgraded Rails in an attempt to fix the problem. I'm at work and don't have access to the logs, but I'll post everything when I get off work later today. I also used irb to get a trace, which is what led me inside ActiveRecord.

    If you have any ideas on what may be happening, I'm all ears. I'd really like to get this resolved.

  9. #9
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a snippet from my development log(the rest are currently nil).

    Code:
    Processing AdminForumController#list (for 127.0.0.1 at 2005-12-20 17:56:50) [GET]
      Parameters: {"action"=>"list", "controller"=>"admin_forum"}
       [4;35;1mAgBoard Count (0.000320) [0m    [0mSELECT COUNT(*) FROM ag_boards  [0m
       [4;36;1mAgBoard Load (0.000225) [0m    [0;1mSELECT * FROM ag_boards LIMIT 0, 10 [0m
    Rendering admin_forum/list
       [4;35;1mAgBoard Columns (0.000255) [0m    [0mSHOW FIELDS FROM ag_boards [0m
    Completed in 0.01366 (73 reqs/sec) | Rendering: 0.00309 (22%) | DB: 0.00562 (41%) | 200 OK [http://localhost/admin_forum/list]
       [4;36;1mAgBoard Load (0.001610) [0m    [0;1mSELECT * FROM ag_boards WHERE (ag_boards.id = 1) LIMIT 1 [0m
       [4;35;1mAgBoard Load (0.000607) [0m    [0mSELECT * FROM ag_boards WHERE (ag_boards.id = 1) LIMIT 1 [0m
       [4;36;1mSQL (0.000136) [0m    [0;1mBEGIN [0m
       [4;35;1mAgBoard Columns (0.000188) [0m    [0mSHOW FIELDS FROM ag_boards [0m
       [4;36;1mAgBoard Load (0.000460) [0m    [0;1mSELECT * FROM ag_boards WHERE (ag_boards.parent_id = 1) ORDER BY title  [0m
       [4;35;1mSQL (0.000124) [0m    [0mROLLBACK [0m
    The destroy method is wrapped in a transaction. As you can see, the destroy is failing and the transaction is being rolled back.

  10. #10
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found the problem.

    has_one :ag_board_attribute

    not
    has_one :AgBoardAttribute


    Thanks for the help everyone


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •