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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:23:52+00:00 2026-06-15T17:23:52+00:00

having a play with scopes tonight in rails 3, and trying to get my

  • 0

having a play with scopes tonight in rails 3, and trying to get my head around what lambda does?

What I am trying to achieve in this example is get a list of country names (:name) from my countries model which is associated with my recipe model. A recipe belongs_to country and a country has many recipes.

I would like to order the recipes by amount of times they appear in a recipe, starting with the highest..

So i am trying this in my recipe model ( or should I do it within the country model?, but then that wouldn’t work would it as my country model is pre populated with 1 instance of every country in the world)

scope :top_countries, lambda { joins(:countries).merge(Country.name).order("name DESC") }

however i get this error message

undefined method `default_scoped?' for "Country":String

my controller

@toprankingcountry = Recipe.top_countries

obviously my understanding is not what i thought and would appreciate some pointers/assistance

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-06-15T17:23:53+00:00Added an answer on June 15, 2026 at 5:23 pm

    I believe the problem is with merge(Country.name) part. joins(:countries) returns ActiveRecord::Relation instance. Its merge method expects one argument which is another instance of a ActievRecord::Relation, whereas you merge string Country.name.

    Generally speaking, getting a list of top countries effectively means getting a list of Countries order by some additional condition. So I’d put this logic into a Country model.

    class Country
      has_many :recipes
    
      def self.top_countries
        joins(:recipes).
          select('countries.*, count(*) AS recipes_count').
          group('countries.id').
          order('recipes_count DESC')
      end
    end
    

    Also if you are using RDBMS which cannot figure out dependent rows on its own (like PostgreSQL < 9.1) you’ll have to manually list all the columns in group by clause.

    get a list of country names (:name) from my countries model which is associated with my recipe model (instance)

    I believe it is impossible to achieve what you described as is because a country has_many recipes, not vice versa

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

Sidebar

Related Questions

I am having a play around with codeigniter and trying to get my head
I'm having a bit of a play around with IIS7, just trying to catch
I'm having a play around with JSON in a simple network environment and trying
I'm having my first play around with Ember.js, and am falling at the first
I'm having a good old play with Redis Webdis Dart I wrote this #import('dart:html');
I'm having trouble getting IE to play nice with my regex. I'm trying to
I was just having a play around with some code in LINQPad and noticed
I have downloaded the Flot library and have been having a play around. I
I'm having trouble making a POST to the play framework - this may not
I'm having a play around with using mod_rewrite to provide a better looking URL

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.