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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:54:48+00:00 2026-05-26T22:54:48+00:00

Consider the following: ScheduledSession ——> Applicant <—— ApplicantSignup Points to note: A ScheduledSession will

  • 0

Consider the following:

ScheduledSession ------> Applicant <------ ApplicantSignup

Points to note:

  1. A ScheduledSession will exist in the system at all times; think of this as a class or course.
  2. The intent here is to validate the ApplicantSignup model against an attribute on ScheduledSession during signups_controller#create

Associations

class ScheduledSession < ActiveRecord::Base
  has_many :applicants, :dependent => :destroy
  has_many :applicant_signups, :through => :applicants
  #...
end

class ApplicantSignup < ActiveRecord::Base
  has_many :applicants, :dependent => :destroy
  has_many :scheduled_sessions, :through => :applicants
  #...
end

class Applicant < ActiveRecord::Base
  belongs_to :scheduled_session
  belongs_to :applicant_signup

  # TODO: enforce validations for presence
  # and uniqueness constraints etc.
  #...
end

SignupsController

Resources are RESTful, i.e. the #create action will have a path that’s similar to /scheduled_sessions/:id/signups/new

def new
  @session = ScheduledSession.find(params[:scheduled_session_id])
  @signup = @session.signups.new
end

def create
  @session = ScheduledSession.find(params[:scheduled_session_id])
  @session.duration = (@session.end.to_time - @session.start.to_time).to_i
  @signup = ApplicantSignup.new(params[:signup].merge(:sessions => [@session]))

  if @signup.save
   # ...
  else
    render :new
  end
end

You’ll notice I’m setting a virtual attribute above @session.duration to prevent Session from being considered invalid. The real ‘magic’ if you will happens in @signup = ApplicantSignup.new(params[:signup].merge(:sessions => [@session])) which now means that in the model I can select from self.scheduled_sessions and access the ScheduledSession this ApplicantSignup is being built against, even though at this very point in time, there is no record present in the join table.

Model validations for example look like

def ensure_session_is_upcoming
  errors[:base] << "Cannot signup for an expired session" unless self.scheduled_sessions.select { |r| r.upcoming? }.size > 0
end

def ensure_published_session
  errors[:base] << "Cannot signup for an unpublished session" if self.scheduled_sessions.any? { |r| r.published == false }
end

def validate_allowed_age
  # raise StandardError, self.scheduled_sessions.inspect
  if self.scheduled_sessions.select { |r| r.allowed_age == "adults" }.size > 0
    errors.add(:dob_year) unless (dob_year.to_i >= Time.now.strftime('%Y').to_i-85 && dob_year.to_i <= Time.now.strftime('%Y').to_i-18)
    # elsif ... == "children"
  end
end  

The above works quite well in development and the validations work as expected — but how does one test with with Factory Girl? I want unit tests to guarantee the business logic I’ve implemented after all — sure, this is after the fact but is still one way of going about TDD.

You’ll notice I’ve got a commented out raise StandardError, self.scheduled_sessions.inspect in the last validation above — this returns [] for self.scheduled_sessions which indicates that my Factory setup is just not right.

One of Many Attempts =)

it "should be able to signup to a session" do
  scheduled_session = Factory.build(:scheduled_session)
  applicant_signup = Factory.build(:applicant_signup)
  applicant = Factory.create(:applicant, :scheduled_session => scheduled_session, :applicant_signup => applicant_signup)
  applicant_signup.should be_valid
end

it "should be able to signup to a session for adults if between 18 and 85 years" do
  scheduled_session = Factory.build(:scheduled_session)
  applicant_signup = Factory.build(:applicant_signup)
  applicant_signup.dob_year = 1983 # 28-years old
  applicant = Factory.create(:applicant, :scheduled_session => scheduled_session, :applicant_signup => applicant_signup)
  applicant_signup.should have(0).error_on(:dob_year)
end

The first one passes, but I honestly do not believe it’s properly validating the applicant_signup model; the fact that self.scheduled_sessions is returning [] simply means that the above just isn’t right.

It’s quite possible that I’m trying to test something outside the scope of Factory Girl, or is there a far better approach to tackling this? Appreciate all comments, advice and constructive criticism!

Updates:

  • Not sure what this is called but this is the approach taken at least with regards to how it’s implemented at the controller level
  • I need to consider ignoring Factory Girl for the association aspect at least and attempt to return the scheduled_session by mocking scheduled_sessions on the applicant_signup model.

Factories

FactoryGirl.define do  
  factory :signup do
    title "Mr."
    first_name "Franklin"
    middle_name "Delano"
    last_name "Roosevelt"
    sequence(:civil_id) {"#{'%012d' %  Random.new.rand((10 ** 11)...(10 ** 12))}"}    
    sequence(:email) {|n| "person#{n}@#{(1..100).to_a.sample}example.com" }
    gender "male"
    dob_year "1980"
    sequence(:phone_number) { |n| "#{'%08d' %  Random.new.rand((10 ** 7)...(10 ** 8))}" }
    address_line1 "some road"
    address_line2 "near a pile of sand"
    occupation "code ninja"
    work_place "Dharma Initiative"
  end

  factory :session do
    title "Example title"
    start DateTime.civil_from_format(:local,2011,12,27,16,0,0)
    duration 90
    language "Arabic"
    slides_language "Arabic & English"
    venue "Main Room"
    audience "Diabetic Adults"
    allowed_age "adults"
    allowed_gender "both"
    capacity 15
    published true
    after_build do |session|
      # signups will be assigned manually on a per test basis
      # session.signups << FactoryGirl.build(:signup, :session => session)
    end  
  end

  factory :applicant do
    association :session
    association :signup
  end

  #...
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-26T22:54:49+00:00Added an answer on May 26, 2026 at 10:54 pm

    My earlier assumption was correct, with on small change:

    I need to consider ignoring Factory Girl for the association aspect at
    least and attempt to return the scheduled_session by stubbing
    scheduled_sessions on the applicant_signup model.

    making my tests quite simply:

    it "should be able to applicant_signup to a scheduled_session" do
      scheduled_session = Factory(:scheduled_session)
      applicant_signup = Factory.build(:applicant_signup)
      applicant_signup.stub!(:scheduled_sessions).and_return{[scheduled_session]}
      applicant_signup.should be_valid
    end
    
    it "should be able to applicant_signup to a scheduled_session for adults if between 18 and 85 years" do
      scheduled_session = Factory(:scheduled_session)
      applicant_signup = Factory.build(:applicant_signup)
      applicant_signup.dob_year = 1983 # 28-years old
      applicant_signup.stub!(:scheduled_sessions).and_return{[scheduled_session]}
      applicant_signup.should have(0).error_on(:dob_year)
      applicant_signup.should be_valid
    end
    

    and this test in particular required a similar approach:

    it "should not be able to applicant_signup if the scheduled_session capacity has been met" do
      scheduled_session = Factory.build(:scheduled_session, :capacity => 3)
      scheduled_session.stub_chain(:applicant_signups, :count).and_return(3)    
      applicant_signup = Factory.build(:applicant_signup)
      applicant_signup.stub!(:scheduled_sessions).and_return{[scheduled_session]}
      applicant_signup.should_not be_valid
    end
    

    …and success — ignore the testing duration as spork causes false reporting of this.

    Finished in 2253.64 seconds
    32 examples, 0 failures, 3 pending
    Done.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following make: all: a b a: echo a exit 1 b: echo b
consider following: 1st APPROACH: public void f3() { f2(); f1(); } and this ...
Consider following statement: C a, b; //C contains c1, c2 and c3 all integers
Consider following code: public sealed class Program { public static void Main() { System.Console.WriteLine(Hi);
consider following codes and classes: using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml.Serialization; namespace
Consider following code: Dim S1 As String = a 'this is the string in
Consider following code: My problem is: 1) I can't seem to cast the errors
Consider following string Some string with quotes and \pre-slashed\ quotes Using regex, I want
Consider following text: $content=<<<EOT { translatorID: f4a5876a-3e53-40e2-9032-d99a30d7a6fc, label: ACL, creator: Nathan Schneider, target: ^https?://(www[.])?aclweb\\.org/anthology-new/[^#]+,
Consider following 2 programs giving same error First calss: public class Testing { Testing

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.