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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:49:57+00:00 2026-05-14T02:49:57+00:00

Im trying to eliminate two tables from my database. The tables are message_sort_options and

  • 0

Im trying to eliminate two tables from my database. The tables are message_sort_options and per_page_options. These tables basically just have 5 records which are options a user can set as their preference in a preferences table. The preferences table has columns like sort_preferences and per_page_preference which both point to a record in the other two tables containing the options. How can i set up the models with virtual attributes and fixed values for the options – eliminating table lookups every time the preferences are looked up?

  • 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-14T02:49:57+00:00Added an answer on May 14, 2026 at 2:49 am

    Create a app_config.yml file in config directory.

    page:
      small: 10
      medium: 20
      large: 30
    
    
    sort:
      name: name DESC  
      amount: amount ASC
      date: created_at DESC  
    

    Create UserOptions class in models directory.

    class UserOptions
      def self.page_option key
        options['page'][key] rescue nil  
      end
    
      def self.sort_option key
        options['sort'][key] rescue nil
      end
    
      def self.options
        @options ||=  YAML.load_file( File.join(RAILS_ROOT, 
                        "config", "app_config.yml")) rescue {}
      end
    
    
      # use this in the view to set the preference
      def self.page_collection
        option_collection 'page'
      end
    
      # use this in the view to set the preference
      def self.sort_collection
        option_collection 'sort'
      end
    
      def self.option_collection key
        (options[key]|| {}).to_a
      end
    end
    

    Configure your models:

    class User
      has_one :preference
    end
    
    class Preference
    
      def sort_preference(default = nil)
        UserOptions.sort_option(attributes['sort_preference']) || default
      end
    
      def per_page_preference(default = nil)
        UserOptions.page_option(attributes['per_page_preference']) || default
      end
    
    end
    

    Now you can do the following:

    current_user.preference.per_page_preference
    
    # use 10 as the page size if no value is given
    current_user.preference.per_page_preference(10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables (tbArea, tbPost) that relate to the following classes. class Area
I'm porting a process which creates a MASSIVE CROSS JOIN of two tables. The
I am trying to reduce/eliminate magic strings from my MVC3 project and was wondering
I'm trying to eliminate memory leaks from my project, but i'm stuck in 2
I'm trying to get a list of IDs from three tables. table_one --------- |
I am trying to eliminate entering each individual choice manually and have the curl
I have some functional code that I'm trying to speed up by eliminating the
I'm trying to create geometry to represent the Earth in OpenGL. I have what's
I am trying to eliminate some redundancy, but I appear to be missing something
I'm trying to eliminate the phenomenon of resending information when a client press the

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.