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 8955499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:34:25+00:00 2026-06-15T14:34:25+00:00

I have an instance method on my Writer class that uses the defined? operator

  • 0

I have an instance method on my Writer class that uses the defined? operator to check and see if posted_posts is defined:

def performance_score
  logger.debug posted_posts
  logger.debug defined?(posted_posts)
  unless defined?(posted_posts)
    ...
  else
    ...
  end
end

In my controller I have a writers instance defined as follows:

@writers = Writer.select("writers.*, p.total_posts, p.posted_posts").joins(...)

When I call @writers.performance_score, from the first logger.debug line it says I have 4 posted_posts, but the second prints out nil. Furthermore, it goes into the unless section instead of the else.

To test this, I stub out the posted_posts to 4. This prints out 4 from the first logger.debug line and the second logger.debug says it is defined as a method. Furthermore, it does go into the else.

Can you please help me understand why this is failing in my Rails application?

  • 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-15T14:34:27+00:00Added an answer on June 15, 2026 at 2:34 pm

    It is likely failing in reality because the relation returned from your AREL select statement uses method_missing magic to return a value from the call. This is not the same thing as being defined. When you mock/stub the “method” on your test object it is likely using the define_method function which is creating a defined function.

    This may do what you want instead.

    if respond_to?(:posted_posts)
      ...
    else
      ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an instance method that I'd like to invoke directly using the callback
I have an IBAction instance method that is connected to a slider, and displays
If I have for example a class with instance method and variables class Foo
I have a method that calls a service to retrieve an instance of an
I have a CsvWriter class that inherits from a Writer class. The Writer class
I have a method on one of my objects that returns a new instance
We have a class in our codebase currently that uses the synchronized keyword at
Say I have a class with synchronized methods. It's actually a class that uses
My Writer model class does not have 'posted_posts' or 'rejected_posts' attributes on it. However,
I have a seed object which has an instance method seed:fall() which is called

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.