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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:53:27+00:00 2026-05-23T04:53:27+00:00

Running Rails 3.0.7 on Mac OS X with Ruby 1.9.2 I have three models

  • 0

Running Rails 3.0.7 on Mac OS X with Ruby 1.9.2

I have three models with tables behind them (sqlite3 for dev, postgres for prod). They are as follows (validations and other irrelevant lines truncated):

class ServiceProvider < ActiveRecord::Base
  has_many :services
  has_many :advertisements, :through => :services
  scope :active, where("service_providers.active = ?", true)
end

class Service < ActiveRecord::Base
  belongs_to :service_provider
  has_many :advertisements
  scope :active, joins(:service_provider).where("services.active = ?", true) & ServiceProvider.active
end

class Advertisement < ActiveRecord::Base
  belongs_to :service
  scope :ranked, where("advertisements.rank > 0")
  scope :current, where("start_date <= ? AND end_date >= ?", Date.today, Date.today)
  scope :service_active, joins(:service) & Service.active
  scope :active, ranked.current.service_active
end

As you can see, each service provider has many services and each service can have many advertisements. The advertisements table has the service_id foreign key in it and the services table has the service_provider_id foreign key in it. All pretty standard many to one relationships. Service Providers can be made inactive through an “active” flag as can individual services in a similar way.

What I want to be able to do is create a named scope on the Advertisement model to give me a list of all advertisements which are ranked and current and who’s parent service is active and in turn who’s service_provider is active. In other words, if a service is made inactive then all advertisements pointing to it should become inactive and if a service provider is made inactive then all services, and in turn all advertisements under those services should become inactive.

I have made a stab at creating the required named scopes above and it works as expected at the Service.active and ServiceProvider.active levels but if I type in Advertisement.active I get an error:

ActiveRecord::ConfigurationError: Association named 'service_provider' was not found; perhaps you misspelled it? 

I think this is because I don’t have service_provider_id as a foreign key field in the advertisers table but I didn’t think I would need that because it should access the service_provider via the service_provider_id field on the services table. It feels like I need a belongs_to :service_provider, :through => :service clause in my Advertisement model but this isn’t possible as far as I know. I have tried various things to get this working but I’m struggling to make progress.

Can anyone tell me a nice elegant way to achieve the above, ideally without changing my table structure? I need it to be quite efficient as this query will be run for every page load on my website (sidebar ads on each page).

Many thanks,
Craig.

  • 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-23T04:53:28+00:00Added an answer on May 23, 2026 at 4:53 am

    I think the following would work:

    class Advertisement
        scope :service_active, joins(:service => :service_provider)
                               .where(:services => { :active => true })
                               .where(:service_providers => { :active => true })
    end
    

    Then you should be able to call

    Advertisement.ranked.current.service_active
    

    and get the expected result.

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

Sidebar

Related Questions

I was trying to get the latest ruby on rails for my Mac running
I am using Apache Ruby and Ruby on Rails 3 Mac Os running Snow
I have an app written in ruby on rails which is running fine on
I have a rails application on RubyMine. While running tests on terminal(Mac OS X
I have an app running Rails 2.3.5 that has a JSON API for much
Running a rails site right now using SQLite3. About once every 500 requests or
I am trying to setup my dev environment on my Mac (running Mac OS
I have site running rails application and resque workers running in production mode, on
When running rails generate I get the following error: /usr/local/rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in require': no such file
My Mac battery ran out while I was running a Rails 3 app, and

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.