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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:12:26+00:00 2026-05-15T05:12:26+00:00

I have a notifications table which contains different types of notifications for different events.

  • 0
  • I have a notifications table which contains different types of notifications for different events.
  • Inside the table is a notifications_type:string column that contains the type of notification, i.e. “foo” or “bar” or “oof”
  • I want the user to be able to select what notifications they want to display, so there are checkboxes below the result that correspond to prefs_display_foo:boolean, prefs_display_bar:boolean in the User model.
  • What is an elegant way for me to set the :conditions in the find to properly display the sorted results? Also, currently I have it as a method in the User model, but how would I do it as a has_many :notifications, :conditions => .....
  • 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-15T05:12:26+00:00Added an answer on May 15, 2026 at 5:12 am

    You could try this

    leave the has_many without any conditions and then create a named_scope to handle the preferences, this code is untested

    class User
        has_many :notifications  
    
        def notification_preferences
            list = []
            list << 'Foo' if prefs_display_foo
            list << 'Bar' if prefs_display_bar    
            # etc...
            return list
        end
    end
    
    class Notification
        belongs_to :user
    
        named_scope :preferred,
            lambda { |preferences| {
                :conditions => ["notifications_type IN (?)", preferences]
                }
            }
    end
    
    # ...
    user = User.find(1)
    notifications = user.notifications.preferred(user.notification_preferences)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a notifications table in rails which contains the columns: notification_type, notification_id
I have a table in database which contains start date and end date. Now
I have an NSTableView that contains 2 different Columns - one is an NSImageCell
I have a model called Notifications, which basically acts as a join table between
Given a Database that has a table Called [Bid Transactions], which contains records of
I have a table which has fields date_of_notification, date_of_loss. Both are of type time-stamp.
I have a view controller that contains a table view, the items in the
I have a table called notification , with two columns: provider email The values
I have a simple MySQL table for storing notification in a web with social
I have code like: @notifications = Notification.find_all_by_user_id(@user.id, :order=>'deliver_by DESC', :conditions=>deliver_by >= '#{Date.today.to_s(:db)}') logger.info @upcoming_reminders[0].inspect

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.