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

  • Home
  • SEARCH
  • 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 8832531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:33:12+00:00 2026-06-14T08:33:12+00:00

This is the weirdest thing ever happened to me with ruby/rails. I have a

  • 0

This is the weirdest thing ever happened to me with ruby/rails.

I have a model, Store, which has_many Balances. And I have a method that gives me the default balance based on the store’s currency.

Store model.

class Store < ActiveRecord::Base

  has_many :balances, as: :balanceable, dependent: :destroy

  def default_balance
    #puts self.inspect <- weird part.
    balances.where(currency: self.currency)[0]
  end
  ...
end

Balance model.

class Balance < ActiveRecord::Base

  belongs_to :balanceable, :polymorphic => true
  ...
end

Ok, so then in the Balance controller I have the show action, that will give me a specific balance or the default one.

Balance controller.

class Api::Stores::BalancesController < Api::Stores::BaseController

  before_filter :load_store

  # Returns a specific alert
  # +URL+:: GET /api/stores/:store_id/balances/:id
  def show
    #puts @store.inspect <- weird part.
    @balance = (params[:id] == "default") ? @store.default_balance : Balance.find(params[:id])
    respond_with @balance, :api_template => :default
  end
  ...

  private
    # Provides a shortcut to access the current store
    def load_store
      @store = Store.find(params[:store_id])
      authorize! :manage, @store
    end
end

Now here is where the weird part comes…

If I make a call to the show action; for example:

GET /api/stores/148/balances/default

It returns null (because the currency was set as null, and there is no Balance with null currency), and the SQL query generated is:

SELECT `balances`.* FROM `balances` WHERE `balances`.`balanceable_id` = 148 AND `balances`.`balanceable_type` = 'Store' AND `balances`.`currency` IS NULL

So I DON’T know why… it is setting the currency as NULL. BUT if in any where in that process I put

puts @store.inspect

or inside the default_balance method:

puts self.inspect

it magically works!!!.

So I don’t know why is that happening?… It seems like the store object is not getting loaded until I “inspect” it or something like that.

Thanks

  • 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-06-14T08:33:14+00:00Added an answer on June 14, 2026 at 8:33 am

    OK finally after a lot of debugging, I found the reason…

    In the Store model I have a method_missing method and I had it like this:

    def method_missing method_name, *args
      if method_name =~ /^(\w+)_togo$/
        send($1, *args).where(togo: true)
      elsif method_name =~ /^(\w+)_tostay$/
        send($1, *args).where(tostay: true)
      end
    end
    

    So when I was calling self.currency it went first to the method_missing and then returned null. What I was missing here was the super call.

    def method_missing method_name, *args
      if method_name =~ /^(\w+)_togo$/
        send($1, *args).where(togo: true)
      elsif method_name =~ /^(\w+)_tostay$/
        send($1, *args).where(tostay: true)
      else
        super
      end
    end
    

    But I continue wondering why after I had called puts @store.inspect or puts self.inspect it worked well?. I mean, why in that case that super call wasn’t needed?

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

Sidebar

Related Questions

This is the weirdest thing that has ever happened to me since I am
Hey this is the weirdest problem i've ever encoutered, I have 2 projects that's
This is literally the weirdest thing I have ever encountered. So, I have a
this is the weirdest error I've ever got on Rails. Any idea what this
This is the weirdest thing ever. So I can see these files and cat
Ok this is the weirdest thing I have seen in a while... I am
This is the weirdest thing I've ever seen. (Dead image removed, was a JPG
This is the weirdest thing I have come across. I am including jQuery as
I have a weirdest thing, in this peace of code a browser adds tags
This is the weirdest error I have ever encountered. This MVC web project was

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.