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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:54:32+00:00 2026-06-15T04:54:32+00:00

My Writer model class does not have ‘posted_posts’ or ‘rejected_posts’ attributes on it. However,

  • 0

My Writer model class does not have ‘posted_posts’ or ‘rejected_posts’ attributes on it. However, with this SQL:

posts = self.posts.select("sum(case when state = 'posted' then 1 else 0 end) as posted_posts, sum(case when state = 'rejected' then 1 else 0 end) as rejected_posts")

I can access those ‘properties’ on the Writer instance. When I using RSpec though, I don’t want to have to make this query, but I am unable to set those properties of the Writer instance because it thinks I am calling a method instead:

writer.rejected_posts = 8 

The above line results in “undefined method ‘rejected_posts'”

To ‘mock’ those properties, I did this:

describe "performance_score" do
    it "should return a score based on the posted_posts and rejected_posts algorithm" do
      class Writer
        attr_accessor :posted_posts, :rejected_posts
      end
      writer = Factory(:writer)
      writer.rejected_posts = 8
      writer.posted_posts = 2
      writer.performance_score.should == 80
    end
  end

My main question is how are these methods being added to the class. How does creating a new Writer class know to ‘sync’ up with my writer factory? I thought I was making a new Writer class for this test, but the strange thing is I still have access to other attributes from my writer factory. Hope this made sense, thanks.

Also, has anyone else done something similar in a test, is this the best way to handle this situation? It’d be nice if you tried to set a property that didn’t exist that it would just create it, like it does in JavaScript.

  • 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-15T04:54:34+00:00Added an answer on June 15, 2026 at 4:54 am

    The thing that is returned from your select query is probably not really a Writer model. It is probably an ActiveRecord::Relation. In order to provide the methods you have assigned values to in the SQL statement, it is probably implementing method_missing and checks some kind of internal hash setup by AREL.

    For mocking in tests, what you did will certainly work. Another option would be to use stub.

    it "should return a score based on the posted_posts and rejected_posts algorithm" do
      writer = Factory(:writer)
      writer.stub(:rejected_posts => 8)
      writer.stub(:posted_posts => 2)
      writer.performance_score.should == 80
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model: class MyModel(db.Model): some_list = db.StringListProperty(indexed=True) some_value = db.StringProperty(indexed=True) and a
I have already read Entity Framework One-To-One Mapping Issues and this is not duplicate
I have a global variable in the class: IloModel model; that is going to
I have an abstract generic view-model that I use as a base-class for several
I've got models like this: class PledgeItem(models.Model): title = models.CharField(...) usd_amount = models.DecimalField(...) class
I have the following model : class A: b = ForeignKey('B') class B: a
I have searched this site top to bottom yet have not found a single
My question does not really have much to do with sqlalchemy but rather with
i have the following model in google app engine: class CustomUsers(db.Model): cuid = db.StringProperty()
I have a Model Representative class Representative< ActiveRecord::Base validates_inclusion_of :gender, :in => Representative.genders def

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.