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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:37:22+00:00 2026-05-12T07:37:22+00:00

I have a class similar to the following: class FruitKinds < ActiveRecord::Base Apple =

  • 0

I have a class similar to the following:

class FruitKinds < ActiveRecord::Base
  Apple = FruitKinds.find(:all).find { |fk|
    fk.fruit_name == :apple.to_s
  }

  # ... other fruits

  # no methods
end

Apple and other specific Fruits are commonly used as default values elsewhere in my application, so I want a handy means to refer to them in an enumerable, static-ish way.

However, there’s a problem. There is a database migration to create the FruitKinds table and populate it with the special Fruits like Apple. When the database migration runs to initialize FruitKinds, rake fails to start because it first loads FruitKinds, which then makes a call to the database, which of course fails since the FruitKinds table is not yet there.

The workaround is to comment out the FruitKinds::* fields while the migration runs, but that is awful and hacky. What’s the best way to do this?

  • 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-12T07:37:22+00:00Added an answer on May 12, 2026 at 7:37 am

    That’s a pretty common gotcha, to the point that I now consider any database access at the class definition level an antipattern. The alternative is to make it a lazy property. The simplest way to do this is simply to make it a class-level method instead of a constant. Note that you can have capitalised methods in Ruby, so you can make it look like a constant if you want:

    class FruitKinds < ActiveRecord::Base
      def self.Apple 
        @apple ||= FruitKinds.find(:all).find { |fk|
          fk.fruit_name == :apple.to_s
        }
      end
    
      # ... other fruits
    
      # no methods
    end
    

    Or if you want to get fancy you can use const_missing to dynamically create the constant the first time it is accessed.

    As a side note, that’s about the most inefficient way possible to find a record by name 😉

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

Sidebar

Related Questions

I have a generic method in a base class similar to the following: protected
I have a model class similar to the following: using System; using System.Collections.Generic; using
I have written a module similar to the following: module One class Two def
I have a JPA entity bean similar to the following: @Entity class License {
I have a class similar to the following: public abstract class Manager<T, TInterface> :
Ok I have a class similar to the following... public class Order { private
I have a class similar to the following: class SomeClass { public: template<typename... Args>
I have a class similar to the following: public class MyProxy : ClientBase<IService>, IService
Suppose I have a class similar to the following: #include <vector> class element {
I have a class structure in C#, similar to the following: [DataContract] class Data

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.