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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:42:37+00:00 2026-05-15T02:42:37+00:00

I am trying to figure out how to display a count for records that

  • 0

I am trying to figure out how to display a count for records that have been created in a table since the last_request_at of a user.

In my view I am counting the notes of a question with the following code:

<% unless @questions.empty? %>
  <% @questions.each do |question| %>
    <%= h(question.notes.count) %>
  end
end

This is happening in the /views/users/show.html.erb file. Instead of counting all the notes for the question, I would only like to count the notes that have been created since the users last_request_at datetime. I don’t neccessarily want to scope notes to display this ‘new notes’ count application wide, just simply in this one instance.

To accomplish I am assuming I need to create a variable in the User#show action and call it in the view but not really sure how to do that.

Other information you may need:

class Note < ActiveRecord::Base
  belongs_to :user
  belongs_to :question
end


class Question < ActiveRecord::Base
  has_many :notes, :dependent => :destroy
  belongs_to :user
end
  • 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-15T02:42:37+00:00Added an answer on May 15, 2026 at 2:42 am

    Just create a named scope and then use it only when it applies:

    class Note < ActiveRecord::Base
      named_scope :added_since, lambda { |time| {
        :conditions => time && [ 'created_at>=?', time ]
      }}
    end
    

    This should only enforce conditions if a time is provided. If you submit a nil time, the default behavior is to scope all notes.

    This way you can do something along the lines of:

    @new_notes = @user.notes.added_since(@user.last_login_at)
    

    Adding a named scope does not alter the default scope.

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

Sidebar

Related Questions

I have been trying to figure out how to display a advertisement on a
im new to android and I've been trying to figure out how to display
i have been busting my head trying to figure this out. I found this
I am trying to figure out the command to display all files created 30
I'm trying to figure out how to display the repeated letter in a string.
I'm trying to figure out the best way to display a long list of
I'm trying to figure out the best approach to display combined tables based on
trying to figure out why this is happening - I have an input text
I've been trying to figure out how to reverse the order of a doubly-linked
Here's something I'm trying to figure out concerning display objects in ActionScript3 / Flex.

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.