-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
between_dates(nil, nil) or between_times(nil, nil) clears method chaining. #87
Comments
I think it's a bug. If both values are |
I was creating a selectbox form where a user can select dates to view the records of a specific period, or of all time by leaving the dates blank. At first I did something like this. # start_date and end_date are given by user input
def expenses_for_normal_user(start_date, end_date)
current_user.expenses.where(active: true).between_dates(start_date, end_date) # this clears method chaining and returns all records
end As of now, I'm checking if parameters are nil and changing whether to call between_dates to get the desired behavior. # start_date and end_date are given by user input
def expenses_for_normal_user(start_date, end_date)
if start_date || end_date
current_user.expenses.where(active: true).between_dates(start_date, end_date)
else
# if both start_date and end_date are nil, don't use between_dates
current_user.expenses.where(active: true)
end
end |
I think that's your best bet. You could also do that a bit shorter: def expenses_for_normal_user(start_date, end_date)
expenses = current_user.expenses.where(active: true)
return expenses if start_date.blank? && end_date.blank?
expenses.between_dates(start_date, end_date)
end |
Thank you for the suggestion! That looks better. |
I found that
between_dates(nil, nil)
orbetween_times(nil, nil)
clears all method chaining. Is this an expected behabior?For example, I have
User
andPost
models. I tried the following on rails console.I expected it to work as follows:
Checked with:
The text was updated successfully, but these errors were encountered: