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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:42:57+00:00 2026-06-12T01:42:57+00:00

Relevant Code: http://pastebin.com/EnLJUJ8G class Task < ActiveRecord::Base after_create :check_room_schedule … scope :for_date, lambda {

  • 0

Relevant Code: http://pastebin.com/EnLJUJ8G

class Task < ActiveRecord::Base
  after_create :check_room_schedule

  ...

  scope :for_date, lambda { |date| where(day: date) }
  scope :for_room, lambda { |room| where(room: room) }

  scope :room_stats, lambda { |room| where(room: room) }
  scope :gear_stats, lambda { |gear| where(gear: gear) } 

  def check_room_schedule
    @tasks = Task.for_date(self.day).for_room(self.room).list_in_asc_order
    @self_position = @tasks.index(self)

    if @tasks.length <= 2
      if @self_position == 0 
        self.notes = "There is another meeting in 
    this room beginning at # {@tasks[1].begin.strftime("%I:%M%P")}."
        self.save
      end
    end
  end

  private

    def self.list_in_asc_order
      order('begin asc')
    end
end

I’m making a small task app. Each task is assigned to a room. Once I add a task, I want to use a callback to check to see if there are tasks in the same room before and or after the task I just added (although my code only handles one edge case right now).

So I decided to use after_create (since the user will manually check for this if they edit it, hence not after_save) so I could use two scopes and a class method to query the tasks on the day, in the room, and order them by time. I then find the object in the array and start using if statements.

I have to explicitly save the object. It works. But it feels weird that I’m doing that. I’m not too experienced (first app), so I’m not sure if this is frowned upon or if it is convention. I’ve searched a bunch and looked through a reference book, but I haven’t see anything this specific.

Thanks.

  • 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-12T01:42:58+00:00Added an answer on June 12, 2026 at 1:42 am

    This looks like a task for before_create to me. If you have to save in your after_* callback, you probably meant to use a before_* callback instead.

    In before_create you wouldn’t have to call save, as the save happens after the callback code runs for you.

    And rather than saving then querying to see if you get 2 or more objects returns, you should be querying for one object that will clash before you save.

    In psuedo code, what you have now:

    after creation
      now that I'm saved, find all tasks in my room and at my time
      did I find more than one?
        Am I the first one?
          yes: add note about another task, then save again
          no: everything is fine, no need to re-save any edits
    

    What you should have:

    before creation
      is there at least 1 task in this room at the same time?
        yes: add note about another task
        no: everything is fine, allow saving without modification
    

    Something more like this:

    before_create :check_room_schedule
    def check_room_schedule
      conflicting_task = Task.for_date(self.day)
                             .for_room(self.room)
                             .where(begin: self.begin) # unsure what logic you need here...
                             .first
      if conflicting_task
        self.notes =
          "There is another meeting in this room beginning at #{conflicting_task.begin.strftime("%I:%M%P")}."
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: Most of my relevant source code is in this pastebin: http://pastebin.com/nhAx1jfG Can anyone
JSFiddle link: http://jsfiddle.net/dqkZN/32/ Here is the relevant code: <div class=categories> <h3> <img src=http://i.imgur.com/t5UXT.gif />
I am trying to use the ImageLoader class from libs-for-android (http://code.google.com/p/libs-for-android/), and am getting
full code here... http://pastebin.com/EEnm8vi3 line 378 is not inserting the sections into the current
Extremely relevant: http://code.google.com/p/facebook-actionscript-api/issues/detail?id=256 Specifically: I've discovered that popups are blocked unless triggered by a
Here is the code relevant to my question: http://jsfiddle.net/mkerny45/V23NK/ As you can see there
Relevant Code Snippet: public class MyPreference extends Preference { public MyPreference(Context context, AttributeSet attrs)
Here's the relevant code: Public class User.cs: public void FindByID(int id) { Parser parser
I'm trying to get the apns daemon at http://code.google.com/p/apnsd/ working, and am having problems
Example site: http://johanberntsson.se/dev/fotosida/ Relevant code: jQuery.fn.center = function () { this.css(position,absolute); this.css(top, (($(window).height() -

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.