I'm overlooking what should be a simple solution. I keep getting the error:
NoMethodError in LifetimesController#index
undefined method `deadline' for #<Class:0x007f88a3c19bb0>
This is because one of the lifetime challenges has a nil :deadline. Adding deadlines are optional.
How can I group_by only the lifetime challenges that have a :deadline present?
controller
def index
@lifetimes = current_user.lifetimes.unaccomplished
@lifetime_months = @lifetimes.group_by { |t| t.deadline.beginning_of_month }
end
model
scope :unaccomplished, -> { where(accomplished: nil) }
schema
create_table "lifetimes", force: true do |t|
t.string "name"
t.date "deadline"
t.boolean "accomplished"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
I was unsuccessful in adding a nil or .present? conditional to any of the above code.
Here's the solution that finally worked:
scope :unaccomplished, -> { where.not(deadline: nil) }
I tried very similar things in the past, but only this seemed to work. Thanks for the help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With