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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:07:24+00:00 2026-05-29T21:07:24+00:00

I have an application that allows users to save events. I would like to

  • 0

I have an application that allows users to save events. I would like to be able to see which events are being saved the most over the past week. To do this, I created a ‘trending’ function in my Event model and then tried accessing that in my view. That returns an error.

_trending.html.erb:

missing attribute: is_public
Extracted source (around line #3):

1: 
2: <% 
3:   events = Event.trending( :timespan => 1.week, :limit => 25 )
4: %>

This is the trending function in my event model. I decided to code it in raw SQL (postgres db) so there would be no confusion about how ActiveRecord is building my query.

event.rb:

def self.trending(options={})
  limit = options[:limit]
  span = options[:timespan]

  earliest_save = (Time.now - span)
  event_sql = %( SELECT events.title, saved_events.event_id, COUNT(user_id) as save_count, MAX(saved_events.created_at) as created 
                 FROM saved_events, events
                 WHERE events.id = saved_events.event_id AND saved_events.is_public = TRUE AND saved_events.created_at >= '#{earliest_save}'
                 GROUP BY saved_events.event_id, events.title
                 ORDER BY save_count DESC, created DESC
                 LIMIT #{limit} )

  find_by_sql( "#{event_sql}" )
end

So the rows that should be returned by this function should only include the fields title, event_id, and save_count. Why is it looking for is_public? I get the impression that it somehow expects an actual instance of an Event object. If that is the case, how do I rewrite this?

Update in response to questions about is_public:
is_public is definitely a column in saved_events. I did run the migration. To isolate this further, I removed the ‘AND saved_events.is_public = TRUE’ from the SQL and still got the same error. The SQL is valid, and I checked that it works in pgAdmin.

  • 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-29T21:07:28+00:00Added an answer on May 29, 2026 at 9:07 pm

    I think the issue stems from the way find_by_sql works, as documented in the API.

    If you call a complicated SQL query which spans multiple tables the columns specified by the SELECT will be attributes of the model, whether or not they are columns of the corresponding table.
    http://apidock.com/rails/ActiveRecord/Base/find_by_sql/class

    I rewrote the query the Rails-way™ and was able to get rid of the SELECT statement. This code worked perfectly.

    time_range = (Time.now - span)..Time.now
    joins(:saved_events).where( :saved_events => { :created_at => time_range, :is_public => true } )
                        .group( "saved_events.event_id, events.id" )
                        .order( "COUNT(saved_events.user_id) DESC, MAX(saved_events.created_at) DESC" )
                        .limit( limit )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that allows users to save files to Isolated Storage. The
I have an application that allows users to write their own code in a
Let say, I have a web application that allows users to upload images and
We have created a web application, using ASP.NET, that allows users to upload documents
I have a really simple Rails application that allows users to register their attendance
I have a script that allows users to 'save as' a pdf, this is
If I have a PHP application which allows users to make changes to documents,
I have created an application that allows users to capture information for entities based
I have an application that allows users to upload files. Some of these files
I have a developed a application that allows users to draw simple images on

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.