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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:30:44+00:00 2026-05-12T18:30:44+00:00

I have a rails site with two models: events and dates events has many

  • 0

I have a rails site with two models: events and dates

events has many dates

events are tagged using acts_as_taggable_on

I’m trying to find all dates (chronological order) for events tagged with a specific tag. I can figure out how to get all the tagged events, but I can’t figure out how to get all of the dates.

The end result I’m looking for is to create a calendar of similar events where events appear on each of their dates.

Hopefully that makes sense, thanks for any and all input!

  • 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-12T18:30:44+00:00Added an answer on May 12, 2026 at 6:30 pm

    As you know Events.tagged_with("my_tag") returns a list of events matching your tags.

    You can then use the map or collect operator on this list to get a list of dates for each event.

    `map{|e| e.dates}`
    

    This returns a list of date arrays, where each array the list of dates associated with the event in that index of the list returned by Events.tagged_with("my_tag")

    To order by date you need to flatten and sort.

    flatten.sort{|a,b| a.created_at <=> b.created_at}
    

    Making the entire method call:

    Events.tagged_with("my_tag").map{|e| e.dates}.flatten.sort{|a,b| a.created_at <=> b.created_at}
    

    You can simplify this with a named scope on dates. It will be a faster query, but I can’t write one for you without knowing more about the structure of your database.

    It would look something like this, assuming dates belong to an event:

    class Date < ActiveRecord::Base
      ...
      named_scope :for_tag, lambda do |tag|
        {:joins => [:tags, :taggings], 
         :conditions => ["tags.name = ? AND tags.id = taggings.tag_id \
           AND taggings.taggable_id = dates.event_id AND \
           taggings.taggable_type = event", tag],
         :order => 'dates.created_at'}
      end  
    end
    

    You could then do Date.for_tag("my_tag") and retrieve the same list, in a much more efficient SQL query.

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

Sidebar

Related Questions

I have a site built in Ruby On Rails which has many ads in
I am creating a site in Ruby on Rails, I have two models a
I have a Rails 2.3.11 app which has two key Models: Activity Transaction Live
I have a Rails 2.1.2 site that only has html templates e.g. jobs.html.erb, so
I'm new to Rails development. I have two models, Decision and Choice. Every decision
I have a Rails-based site with a basic two-column layout. On the right is
I'm trying to add a Google +1 button to my site. I'm using rails,
I have a Rails site, where the content is written in markdown. I wish
I have a staging Rails site up that's running on MySQL 5.0.32-Debian. On this
I have a Rails app hosted in Heroku. If I go the the site

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.