Ruby on rails关联记录返回空但在rails控制台中它返回一些东西

时间:2016-10-14 09:12:31

标签: ruby-on-rails rails-activerecord model-associations

在registration.rb

class Registration < ActiveRecord::Base
......
has_many :cash_transactions

after_destroy :destroy_cash_transaction

def first_cash_transaction
  self.cash_transactions.first
end

def destroy_cash_transaction
  if first_cash_transaction.registrations.count == 0
   self.cash_transactions.find(first_cash_transaction).destroy
  end
end

in cashtransaction.rb

class CashTransaction< ActiveRecord::Base
......
has_many :registrations

所以,当我登记注册时,它会与注册相关联的destroy_cash_transaction。

在rails控制台中,我尝试了:

Registration.find(some_id).cash_transactions

控制台返回一些记录

但是,当我尝试删除注册时,它将调用destroy_cash_transaction,它将调用first_cash_transaction,它将返回空记录。

任何人都可以帮我吗?非常感谢你。

----- ------更新

参与者控制器中的

 def destroy
  if @registration.destroy
   respond_to do |format|
    format.html {redirect_to admin_events_participants_path, :notice => "#{@registration.email}'s registration has been deleted."}
   end
  else
   respond_to do |format|
    format.html {
     flash[:warning] = print_out_message('form-destroy','registration')
     redirect_to admin_events_participants_path 
    }
   end
  end

def load_registration
  @registration = @event.registrations.find(params[:registration_id])
 end

0 个答案:

没有答案