For the sake of explanation, I'm writing an app where a User can log their expenses.
In the User's show view, I want to only show the User's expenses from the current month.
My expenses table looks like this:
create_table "expenses", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Thu, 14 Apr 2016
month = Date.today.strftime("%m")
@user = User.find(params[:id])
@expenses = Expense.where(:user_id => @user.id, :date => month)
Usually you can tackle it this way:
Where that defines a range that can be used as a
BETWEEN x AND y clause.
If this is a common operation you might want to express the date as a separate column in
YYYYMM format so that these are easily retrieved.