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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:16:31+00:00 2026-05-16T22:16:31+00:00

I need to have one table in which i can set user preferences I

  • 0

I need to have one table in which i can set user preferences I want to have one table in which all preferences values are set and another relation table in which i will have user_id and the preference_id but I need to have preference for three different pages

  1. item
  2. favorite
  3. recent.

I need to show the list of results as set by user for e.g if on item page user wants 20 results to be shown then I want to set prference id set as 1 which as value 20.

prefrence table 
id
value

user_preferences 
id
item_result (foreign_key prefernce_id)
favorite_result (foreign_key preference_id)
recent_result (foreign_key preference_id)

I want something which will allow me to extract values like we do user.item.name
same way I want to do user_preference.item_result.value

  • 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-16T22:16:32+00:00Added an answer on May 16, 2026 at 10:16 pm

    I resolved the issue on my own..
    Before posting answer I must say everyone should user rails console

    I have define relations as follows

    class User < AR::Base
      has_one :item_preference
      has_one :item_preference, :through => :user_preference, :source => :item
    
      has_one :favorite_preference
      has_one :favorite_preference, :through => :user_preference, :source => :favorite
    
      has_one :recent_preference
      has_one :recent_preference, :through => :user_preference, :source => :recent
    
      has_one :user_preference
    end
    
    class UserPreference < ActiveRecord::Base
      belongs_to :item,
      :foreign_key => "item_results",
      :class_name => "Preference"
    
      belongs_to :favorite,
      :foreign_key => "favorite_results",
      :class_name => "Preference"
    
      belongs_to :recent,
      :foreign_key => "recent_results",
      :class_name => "Preference"
    
      belongs_to :user
    end
    
    class Preference < ActiveRecord::Base
      has_many :item_preferences,
        :foreign_key => "item_results",
        :class_name => "UserPreference"
    
      has_many :favorite_preferences,
        :foreign_key => "favorite_results",
        :class_name => "UserPreference"
    
      has_many :recent_preferences,
        :foreign_key => "recent_results",
        :class_name => "UserPreference"
    
    end  
    

    now I can access the values as

    User = User.find(16)
    user.item_preference
    [:] [DEBUG] [2010-09-16 17:09:54] [           App::OnlineOrdering] -   Preference Load (0.4ms)   SELECT `preferences`.* FROM `preferences` INNER JOIN `user_preferences` ON `preferences`.id = `user_preferences`.item_results WHERE ((`user_preferences`.user_id = 16)) 
    => #<Preference id: 1, value: "20", created_at: "2010-09-15 15:13:04", updated_at: "2010-09-15 15:13:04">
    

    and other way around

    preference = Preference.find(1)
    
    preference.item_preferences
    [:] [DEBUG] [2010-09-16 17:09:23] [           App::OnlineOrdering] -   UserPreference Load (0.4ms)   SELECT * FROM `user_preferences` WHERE (`user_preferences`.item_results = 1) 
    [:] [DEBUG] [2010-09-16 17:09:23] [           App::OnlineOrdering] -   UserPreference Columns (1.2ms)   SHOW FIELDS FROM `user_preferences`
    => [#<UserPreference id: 1, user_id: 16, item_results: 1, favorite_results: 2, recent_results: 2, created_at: "2010-09-15 15:13:04", updated_at: "2010-09-15 15:13:04">]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have one column as the primary key and another to auto
Related to https://stackoverflow.com/questions/139944/where-can-one-find-free-software-icons-images I have a need for free weather-related icons. Specifically, I need
I have two tables with the same columns, and I need to copy one
I have one field that I need to sum lets say named items However
I have one JSON that is coming in a string format. I need to
Why do I need two of them? When I have to use one or
I have a JavaScript method that I need to run on one of my
I have a scenario where I need to upload a file from one web
I have a DataGridView with one DataGridViewComboBoxColumn in my WinForms application. I need to
I have huge number of Word files I need to merge (join) into one

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.