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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:49:46+00:00 2026-05-31T23:49:46+00:00

In my model, I dynamically create some methods based on database records: class Job

  • 0

In my model, I dynamically create some methods based on database records:

class Job < ActiveRecord::Base
  belongs_to :job_status

  # Adds #requisition?, #open?, #paused?, #closed?
  class_eval do    
    JobStatus.all.each do |status|
      unless method_defined? "#{status.name.downcase}?"
        define_method("#{status.name.downcase}?") do 
          job_status_id == status.id
        end
      end
    end
  end
end

class JobStatus < ActiveRecord::Base
  has_many :jobs
end

The job_statuses table contains some seed data, so is not going to be frequently changing, but in case I ever need to add new statuses, I don’t have to add more code to get a boolean method for the new status.

However, I am not sure how to test these methods, because when rspec starts the job_statuses table is obviously empty, and when the JobStatus objects are created, Job gets initialized, but since no objects exist yet, it doesn’t create any methods, and my tests fail because the methods don’t exist.

Note that I am using rspec with spork & guard, and using database-cleaner with the truncation strategy (as per Railscast #257, since I’m using Selenium), so that probably complicates matters.

  • 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-05-31T23:49:47+00:00Added an answer on May 31, 2026 at 11:49 pm

    The solution I came up with was to abstract the creation of runtime methods out into a library file, and then in my test file, remove and redeclare my class before each test, and reload the actual class (and blueprints) at the end of the suite:

    describe AssociationPredicate do
      before(:all) do
        ["Continuous", "Standard"].each { |type| JobType.create!(:job_type => type) }
        ["Requisition", "Open", "Paused", "Closed"].each { |status| JobStatus.create!(:job_status => status) }
      end
    
      after(:all) do
        DatabaseCleaner.clean_with :truncation, :only => %w( job_types job_statuses )
    
        # Reload Job model to remove changes
        Object.send(:remove_const, 'Job')
        load 'job.rb'
        load 'support/blueprints.rb'
      end
    
      before(:each) do
        Object.send(:remove_const, 'Job')
    
        # Redefine Job model for testing purposes
        class Job < ActiveRecord::Base
          belongs_to  :job_type
          belongs_to  :job_status
    
          has_many    :job_applications
        end
      end
    
      it "should add methods when included" do
        Job.send(:association_predicate, :job_type)
        job.should respond_to(:continuous?)
        job.should respond_to(:standard?)
      end
    end
    

    This way, I create a basic class for each test, add the runtime methods as necessarily, and return to the actual class when I’m done.

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

Sidebar

Related Questions

Ladies & Gentlemen, Short Version: Trying to dynamically create a Spine Model Class. Basically
I want to create my Model fields dynamically (in ExtJS 4). For instance, sometimes
I have functionality where I dynamically create Dialog. Some time I require Modal or
I'm trying to create some modal windows that load in their content dynamically using
I'm trying to model bind a set of dynamically generated checkboxes so as to
#model class Promotion(models.Model): name = models.CharField(max_length=200) start_date = models.DateTimeField() end_date = models.DateTimeField() #view def
model.py: class Tribes(Group): members = models.ManyToManyField(User, related_name='tribes', verbose_name=_('members')) i want to store a number
Model: class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time = models.DateTimeField() def __unicode__(self):
Model for xmlconverter: [XmlRoot(ElementName = location, IsNullable = true)] public class location { public
I've been struggling to create a form for a Mongoid model that has an

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.