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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:50:13+00:00 2026-05-14T08:50:13+00:00

I just got referred to stackoverflow by a friend here to help with a

  • 0

I just got referred to stackoverflow by a friend here to help with a problem I am having. I am fairly new to ruby on rails and I am working on a collaborative project where we have a script (medal_worker.rb) that is scheduled to run at a fixed intervals to award people various medals based on various participation and success on our website. One of the new medals I am working on rewards people for “milestones”. For the purpose of this problem, let’s say we want to give them medals when they make 100, 1000, and 10000 comments. I would like to do this by using named_scopes from the User model (user.rb) to give me filtered lists of the users I am looking for.

My question is: How do I find the users who do not have the respective medals for the respective milestone comment level (preferably using the named_scopes from the User model)?

Here is an exerpt from my model_worker.rb file:

def award_comment_milestone(comments)
   users = Users.frequent_comment_club_members(comments).not_awarded_medal(Medal.find_by_id(medal.id))
   for user in users do
      award_medal(medal, nil, user) if @award
   end
end

Here is where I am at with the named_scopes in the user model (user.rb):

named_scope :frequent_comment_club_members, lambda { |*args|
        {:include => comment_records, :conditions => ['comment_records.comment_type = ? and comment_records.comments >= ?', 'User', (args.first || 0)]}
}

named_scope :not_awarded_medal, lambda { |medal|
    {:include => :awards, :conditions => ['awards.medal_id not in (select awards.medal_id from awards where awards.medal_id = ?)", medal.id] }
}

This is not working as I would like, but I don’t know if the problem is in the named_scopes or how I am passing arguements or what. Thanks.

  • 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-14T08:50:14+00:00Added an answer on May 14, 2026 at 8:50 am

    Your named_scopes look fine. Except you are starting with a single apostrophe and ending with a double apostrophe in the not_awarded_medal condition statement.

    EDIT:

    Take it back. Your not_awarded_medal named_scope is off.

    Try something like this:

    named_scope :not_awarded_medal, lambda { |medal_id|
      { :include => :awards,
        :conditions => [
          "? not in (select awards.id from awards where awards.user_id = users.id)", medal_id
        ]
      }
    }
    

    untested

    Now this is assuming that you have the following relationships:

    User: has_many :awards
    Award: belongs_to :user

    If you are using has_many :through then you are going to have to change the SQL to look at the join (users_awards) table.

    —

    But I do see a couple of things in the award_comment_milestone function.

    What is the parameter coming into award_comment_milestone? Is it an array of comments or is it a count of comments? Also where is medal defined?

    If comments is an array then you need to pass comments.length into frequent_comment_club_members. If it’s the count then I would rename it to comments_count so the next person can understand the logic more quickly.

    Some general observations:

    • not_awarded_medal should just take a medal_id and not the whole object (no need to do multiple queries)
    • Why are you doing Medal.find_by_id(medal.id)? You already have the medal object.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just got a new macbook pro and im having troubles installing rails.. I got
just got a slight problem here with updating a MySql DataGridView from visual studio
Just got the new iPad, and when I touch the microphone and say a
Just got an Arduino and I'm messing around having some problems with the lights.
Just got a new Windows laptop, installing emacs23 on it. My previous computers have
just got a couple of new PanelPC's running Windows Embedded 2009 that we are
Just got a new website project for my company internal use. The whole website
Just got a request from my boss for an application I'm working on. Basically
Just got a strange error. Working on project using JUnit and Maven 3.0.3 I've
Just got a brand new mac. However when I try to install netbeans 7.0.1,

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.