{"record":{"id":"963b022cab76e7ac","repo":"opf/openproject","slug":"couldn-t-find-weekday-with-day-day","errorCode":null,"errorMessage":"Couldn't find WeekDay with day #{day}","messagePattern":"Couldn't find WeekDay with day #(.+?)","errorType":"http","errorClass":"ActiveRecord::RecordNotFound","httpStatus":404,"severity":"error","filePath":"app/models/week_day.rb","lineNumber":38,"sourceCode":"# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, write to the Free Software\n# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n#\n# See COPYRIGHT and LICENSE files for more details.\n#++\n\nclass WeekDay\n  DAY_RANGE = Array(1..7)\n\n  attr_accessor :day\n\n  class << self\n    def find_by!(day:)\n      raise ActiveRecord::RecordNotFound, \"Couldn't find WeekDay with day #{day}\" unless day.in?(DAY_RANGE)\n\n      new(day:)\n    end\n\n    def all\n      DAY_RANGE.map do |day|\n        new(day:)\n      end\n    end\n  end\n\n  def initialize(day:)\n    self.day = day\n  end\n\n  def name\n    day_names = I18n.t(\"date.day_names\")\n    day_names[day % 7]","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/opf/openproject/blob/d9742c43f3424c34b63550f8c03f201fe5c3040c/app/models/week_day.rb#L20-L56","documentation":"WeekDay is a plain Ruby value class (not ActiveRecord) representing weekdays, with DAY_RANGE = 1..7. The class-level find_by! mimics ActiveRecord finder semantics but simply raises ActiveRecord::RecordNotFound unless the given day is within 1..7. Note the convention: OpenProject's WeekDay numbering is Monday=1..Sunday=7, matching Date#cwday, not Date#wday (Sunday=0).","triggerScenarios":"Calling WeekDay.find_by!(day: 0) or find_by!(day: 8) — most commonly by passing Date#wday (0..6, Sunday=0) instead of Date#cwday (1..7, Monday=1), e.g. in non-working-day calculations for duration/working-hours logic.","commonSituations":"Developers convert Ruby's standard wday to this API without remapping the index; parsing day numbers from external systems that use 0-based weeks; off-by-one when looping 0..7 over days.","solutions":["Pass a commercial weekday: use date.cwday (1..7) instead of date.wday (0..6).","If your input is 0-based wday, convert first: wday.zero? ? 7 : wday.","Guard user/system input with day.between?(1, 7) before calling find_by!."],"exampleFix":"# before\nWeekDay.find_by!(day: date.wday)   # raises for Sunday (0)\n\n# after\nWeekDay.find_by!(day: date.cwday)  # Monday=1..Sunday=7","handlingStrategy":"validation","validationCode":"raise ArgumentError, \"day must be 1..7, got #{day}\" unless day.is_a?(Integer) && day.between?(1, 7)","typeGuard":"def week_day_number?(value)\n  value.is_a?(Integer) && value.between?(1, 7)\nend","tryCatchPattern":"begin\n  WeekDay.find_by!(day: day)\nrescue ActiveRecord::RecordNotFound\n  WeekDay.find_by!(day: 1) # or map wday: value.zero? ? 7 : value\nend","preventionTips":["Standardize on Date#cwday when interfacing with WeekDay; never pass Date#wday.","When converting 0-based weekdays: day.zero? ? 7 : day.","Validate external day numbers at the boundary (1..7) before they reach working-day math."],"tags":["date-handling","working-days","validation","record-not-found"],"backgroundTag":"record-not-found","analyzedSha":"d9742c43f3424c34b63550f8c03f201fe5c3040c","analyzedAt":"2026-08-21T14:40:06.829Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}