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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:29:47+00:00 2026-06-15T09:29:47+00:00

Having two classes like this: class Site < ActiveRecord::Base has_one :subscription, dependent: :destroy def

  • 0

Having two classes like this:

class Site < ActiveRecord::Base
  has_one :subscription, dependent: :destroy

  def self.hostname_active?(hostname)
    site = where(hostname: hostname)
    site.exists? && site.first.active?
  end

  def active?
    subscription.active?
  end
end

class Subscription < ActiveRecord::Base
  belongs_to :site

  def active?
    (starts_at..ends_at).cover?(Date.current)
  end
end

describe Site do
  let(:site) { Fabricate.build(:site) }

  describe "#hostname_active?" do
    it "Returns true if site with hostname exists & is active" do
      described_class.stub_chain(:where, :exists?).and_return(true)
      described_class.stub_chain(:where, :first) { site }
      site.stub(:active?).and_return(true)
      described_class.hostname_active?('www.example.com').should be_true
    end

    it "Returns false if site with hostname doesn't exist" do
      described_class.stub_chain(:where, :exists?).and_return(false)
      described_class.stub_chain(:where, :first) { site }
      site.stub(:active?).and_return(false)
      described_class.hostname_active?('www.example.com').should be_false
    end

    it "Returns false if site is not active" do
      described_class.stub_chain(:where, :exists?).and_return(true)
      described_class.stub_chain(:where, :first) { site }
      site.stub(:active?).and_return(false)
      described_class.hostname_active?('www.example.com').should be_false
    end
  end
end

Where the related subscription determines if the Site is active or not, I use the method hostname_active? both as a constraint in routes and also in other classes where I need to determine if it a) exists, and b) is active.

Taken from another question on SO:

Tell-don’t-ask basically means you shouldn’t query an object about it state, make a decision based on it’s state and then tell the same object what to to. If the object has all the information it needs, it should decide for itself.

While I don’t do that, my code does feel pretty coupled, both in regards to the coupling between site & subscription, but also the coupling to ActiveRecord, which makes it hard to test without touching the database.

How would you structure it to avoid asking the related subscription in order to determine the site’s state? And also, would you consider this to be a violation of tell don’t ask?

  • 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-15T09:29:48+00:00Added an answer on June 15, 2026 at 9:29 am

    Using ActiveRecord, you’re going to have some coupling, and it’s okay, and what you are doing does not violate LOD. You can denormalize the active field to keep it on the site itself, but I wouldn’t do that.

    One thing that I would change is to eager load the subscription.

    #Just showing changes
    
    class Site < ActiveRecord::Base
      scope :active, includes(:subscription).merge(Subscription.active)
      has_one :subscription, dependent: :destroy
    
      def self.hostname_active?(hostname)
        active.where(hostname: hostname).exists?
      end
    end
    
    class Subscription < ActiveRecord::Base
      scope :active, where(arel_table[:starts_at].lteq(Date.current), arel_table[:ends_at].gteq(Date.current))
    end
    

    At the very least, that will keep you from having to do two queries to determine if a hostname is active.

    As far as stubbing ActiveRecord, I don’t normally see that happening. The generally accepted practice is to use fixtures or factories to build your test objects. Personally, I use FactoryGirl: https://github.com/thoughtbot/factory_girl_rails.

    In your case, I would have factories like:

    FactoryGirl.define do
      factory :subscription do
        site
        factory :active_subscription do
          starts_at { Date.today.beginning_of_month }
          ends_at { Date.today.end_of_month }
        end
    
        factory :inactive_subscription do
          starts_at { Date.today.beginning_of_month - 3.months }
          ends_at { Date.today.end_of_month - 3.months }
        end
      end
    end
    
    FactoryGirl.define do
      factory :site do
        sequence(:hostname, 1000) {|h| "site#{h}.example.com" }
        factory :active_site do
          after(:create) do |site|
            FactoryGirl.create(:active_subscription, site: site)
          end
        end
        factory :inactive_site do
          after(:create) do |site|
            FactoryGirl.create(:inactive_subscription, site: site)
          end
        end
      end
    end
    

    That would allow my specs to look like:

    describe Site do 
      describe "Active site" do
        subject(:site) { FactoryGirl.create :active_site }
        its(:active?) { should eq(true) }
      end
    
      #etc...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I am having is two classes which go like this. public class Class1
I have two classes like this (simplified and renamed): class Base { public: virtual
Just like the title says I am having problems combining two classes. Altough this
having this two classes I always have a doubt of which is the best
Having the following classes, in two different assemblies: class Member { public string Label
I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub .
Having two classes like Blog and Post, in Entity Framework (and LINQ-to-Entities), how can
I'd like to make two base classes (e.g. figure and move) with some children
I'm having trouble grasping generic methods. I have two classes that are generated (they
i'm writing two anidated classes one is a dict the other a list? having

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.