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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:24:33+00:00 2026-05-26T00:24:33+00:00

I’m working through the RailsTutorial but making an Announcements webapp for the middle school

  • 0

I’m working through the RailsTutorial but making an “Announcements” webapp for the middle school I teach at (tweaking the given Twitter clone).

When users create an announcement, they use check boxes to determine which grades it should be displayed to (1-3 grades could be true). This is working correctly, with me storing grades as booleans.

create_table "announcements", :force => true do |t|
t.string   "content"
t.integer  "user_id"
t.boolean  "grade_6"
t.boolean  "grade_7"
t.boolean  "grade_8"
t.date     "start_date"
t.date     "end_date"
t.datetime "created_at"
t.datetime "updated_at"

end

My users also have a grade field, which is an integer. I want to use this to make each user’s home page show the announcements for their grade.

Example: An 8th grade teacher has grade = 8. When they log in, their home page should only show announcements which have grade_8 = TRUE.

Example: An principal has grade = 0. When they log in, their home page should show all announcements.

I’m struggling with how to translate the integer user.grade value into boolean flags for pulling announcements from the model.

The code I’m writing is working, but incredibly clunky. Please help me make something more elegant! I’m not tied to this db model, if you have a better idea. (In fact, I really don’t like this db model as I’m hardcoding the number of grades in a number of locations).

# Code to pull announcements for the home page
def feed
case grade
when 6
  grade_6
...
else
  grade_all
end 
end

# Example function to pull announcements for a grade
def grade_6
  Announcement.where("grade_6 = ? AND start_date >= ? AND end_date <= ?", 
                     TRUE, Date.current, Date.current)
  • 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-26T00:24:34+00:00Added an answer on May 26, 2026 at 12:24 am

    the correct way to set this type of relationship up would be to use a many-to-many relationship via has_many through:

    class Announcement < ActiveRecord::Base
      has_many :announcement_grades
      has_many :grades, :through => :announcement_grades
    end
    
    class AnnouncementGrades < ActiveRecord::Base
      belongs_to :grade
      belongs_to :announcement
    end
    
    class Grade < ActiveRecord::Base
      has_many :announcement_grades
      has_many :announcements, :through => :announcement_grades
    end
    

    then your migrations will be:

    create_table :announcements, :force => true do |t|
     t.date :start_date
     t.date :end_date
     t.timestamps #handy function to get created_at/updated_at
    end
    create_table :announcement_grades, :force => true do |t|
     t.integer :grade_id
     t.integer :announcement_id
     t.timestamps
     #start and end date might be more appropriate here so that you can control when to start and stop a particular announcement by grade rather than the whole announcement globally, depending on your needs.
    end
    create_table :grades, :force => true do |t|
      t.timestamps
      #now you have a bona-fide grade object, so you can store other attributes of the grade or create a relationship to teachers, or something like that
    end
    

    so, now you can simply find your grade then call announcements to filter:

    @grade = Grade.find(params[:id])
    @announcements = @grade.announcements
    

    so, that’s the correct way to do it from a modeling perspective. there are other considerations to this refactor as you will have to make significant changes to your forms and controllers to support this paradigm, but this will also allow for much greater flexibility and robustness if you decide you want to attach other types of objects to a grade besides just announcements. this railscast demonstrates how to manage more than one model through a single form using nested form elements, this will help you keep the look and feel the same after you apply the changes to your models. I hope this helps, let me know if you need more help doing this, it’ll be a bit of work, but well worth it in the end.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I
I am trying to loop through a bunch of documents I have to put
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker

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.