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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:52:07+00:00 2026-05-24T13:52:07+00:00

I have a 3 models class Task < ActiveRecord::Base belongs_to :user, :dependent => :destroy

  • 0

I have a 3 models

class Task < ActiveRecord::Base
  belongs_to :user, :dependent => :destroy
  has_many :clock_ins

  accepts_nested_attributes_for :clock_ins, :allow_destroy => true
end

class ClockIn < ActiveRecord::Base
  belongs_to :task, :dependent => :destroy
  has_one :clock_out
end

class ClockOut < ActiveRecord::Base
  belongs_to :clock_in, :dependent => :destroy
end

Currently I can create a ClockIn for each Task.

When I start a new ClockIn, I want to create a ClockOut for whichever Task is currently open.

How do I search my tasks for one with a ClockIn that does not have a ClockOut?


Solution

  • Combine Models

  • Fix destroys

  • Iterate all tasks then update task.clocks.where(:clock_out => nil).first.update_attribute :clock_out, Time.now

  • 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-24T13:52:08+00:00Added an answer on May 24, 2026 at 1:52 pm

    As you have a one-to-one relation between clock_in and clock_out, switching has_one and belongs_to shouldn’t make much difference. What is the data stored in clock_in and clock_out? if is is just a datetime you might want to consider merging the two models and using a single table. If you do not want to change any of the modeling LEFT OUTER JOIN is the way to go. So you have three options:

    • Merge the models:

      class Task < ActiveRecord::Base
        belongs_to :user
        has_many :working_hours
      
        accepts_nested_attributes_for :working_hours, :allow_destroy => true
      end
      
      class WorkingHour < ActiveRecord::Base
        belongs_to :task
      
        # has two columns clock_in_time and clock_out_time
      end
      
      task.working_hours.where(:clock_out_time => nil).first.update_attribute(:clock_out_time => Time.now)
      
    • Switch has_one and belongs_to

      class ClockIn < ActiveRecord::Base
        belongs_to :task
        belongs_to :clock_out
        # now clock_ins will have column check_out_id
      end
      
      class ClockOut < ActiveRecord::Base
        has_one :clock_in
        # doen't have any check_in_id
      end
      
      task.clock_ins.where(:clock_out_id => nil).first.create_clock_out(:time => Time.now)
      
    • Go with the outer join

      class ClockIn < ActiveRecord::Base
        belongs_to :task
        has_one :clock_out
      
        scope :has_no_check_out, {
          :joins => "LEFT OUTER JOIN clock_outs ON clock_ins.id = clock_outs.clock_in_id"
          :conditions => "clock_outs.clock_in_id IS NULL"
        }
      end
      
      task.clock_ins.has_no_check_out.first.create_clock_out(:time => Time.now)
      

    Please note that your :dependent => :destroy definitions don’t look very good. Currently if you destroy a clock_out, corresponding clock_in will be destroyed, resulting in corresponding task being destroyed and leaving other clock_ins related with the task orphaned. Also when the task is destroyed, it will result in user being destroyed. This seems to be very odd chain of events. Destroying a clock_out, results in destroying a user, Ouch!

    You should use :dependent => :destroy like following:

    # user.rb
    has_many :tasks, :dependent => :destroy
    
    # task.rb
    has_many :clock_ins, :dependent => :destroy
    
    # clock_in.rb
    has_one :clock_out, :dependent => :destroy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have 3 models class User < ... belongs_to :language has_many :posts end class
I have the following code in my models: Class Farm < ActiveRecord::Base has_many :farm_products,
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have a simple model class Task < ActiveRecord::Base validates :deadline, :if => :deadline_in_future?
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
i have two models: class Category has many :jobs end class Job belongs_to :category

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.