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

  • Home
  • SEARCH
  • 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 7545251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:52:47+00:00 2026-05-30T08:52:47+00:00

I have the following objects and relationships, Lecture >- Tests Test >- Questions Business

  • 0

I have the following objects and relationships,

Lecture >- Tests
Test >- Questions

Business rules

When the lecture is started, a test can be given
If a test is being given, questions can be asked

Inference

Therefore questions shouldn't be asked if the lecture hasn't been started.

Question Model

class Question
  belongs_to :test
  belongs_to :lecture, :through => :test

  def ask_question
    raise "Test not started!" unless test.started?
    raise "Lecture not started!" unless lecture.started?
  end
end

So clearly the state of the question model is now coupled to the state of test and class.

When creating unit tests, to test this I need to set up all this state, which gets quite unwieldy, especially as the business cases get more and more complicated.

How can I avoid this?

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

    I’m not experinced with Ruby associations, but it seems to me that somehow data model is cofused with run-time logic here.

    If I’d make a data model for question and tests, I’d want to re-use my questions across tests and also re-use prepared tests (sets of questions) across lectures. In that case I’d write something like

    class Lecture
      has_and_belongs_to_many :tests
    end
    
    class Test
      has_and_belongs_to_many :lectures
      has_and_belongs_to_many :questions
    end
    
    class Question
      has_and_belongs_to_many :tests
    end
    

    Separately from that structure I’d have some structure corresponding to real-time lectures, tests, questions and a notion of a result. A result is an attempt to answer a real-time question by a given student.

    I’d also “delegate” the check of the lecture session state to the test session. If test session cannot be started for whatever reason the question session cannot be started too.

    To unit-test a question session you will only need to mock a test session, to unit test a test session you will need to mock a lecture session, and so on.

    class Lecture_Session
      has_many :tests_sessions
      belongs_to :lecture
    end
    
    class Test_Session
      belongs_to :lecture_session
      belongs_to :test
      has_many :question_sessions
    
      def validate
        raise "Lecture not started!" unless lecture_session.started?
      end
    end
    
    class Question_Session
      belongs_to :question
      belongs_to :test_session
    
      def validate
        raise "Test not started!" unless test_session.started?
      end
    end
    
    class Result
      belongs_to :lecture_session
      belongs_to :test_session
      belongs_to :question_session
      belongs_to :student
    
      def validate
        raise "Question is not active!" unless question_session.active?
      end
    end
    

    Hope this helps.

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

Sidebar

Related Questions

Given I have two File objects I can think of the following implementation: public
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and
I have the following objects. An IEnumerable <Agency > Agencies, which then contains BusinessUnits
I currently have the following objects persisting successfully: Person first name, etc. Exams title,
Let's say I have the following objects: squirrel_table - name - country_of_origin - id
I have the following class objects: public class VacancyCategory { public int ID {
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
I have the following code in my index view. latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10] for entry
I have a database where I store objects. I have the following (simplified) schema
In my django views i have the following def create(request): query=header.objects.filter(id=a)[0] a=query.criteria_set.all() logging.debug(a.details) I

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.