Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Find entries by date
    primarykey
    data
    text
    <p>I'm running Ruby 2 and Rails 4.</p> <p>I am creating an app where you can track the calories and macronutrients of each meal entered on a particular day, but I would like the ability to look back at previous days.</p> <p>I have a table showing what meals have been entered today on my index page through a scope in my model. How would I implement the ability to look back at other days in Rails? </p> <p>Should I add in a date model or give each meal a datetime? Is there a gem for this sort of thing?</p> <p><strong>Meals Model</strong></p> <pre><code>class Meal &lt; ActiveRecord::Base scope :today, lambda { where("created_at &gt; ?", Time.now.beginning_of_day) .where("created_at &lt; ?", Time.now.end_of_day) } scope :previous, lambda { where("created_at &lt; ?", Time.now.beginning_of_day) } validates :name, presence: true validates :calories, presence: true, numericality: { only validates :protein, numericality: { only_integer: true } validates :carbohydrates, numericality: { only_integer: t validates :fats, numericality: { only_integer: true } belongs_to :user end </code></pre> <p><strong>Meals Controller</strong></p> <pre><code> def index if user_signed_in? @todays_meals = current_user.meals.today unless current_user.bmr.blank? || current_user.weight.blank? || current_user.protein_intake.blank? || current_user.fat_percentage.blank? @remaining_calories = (current_user.bmr) - @todays_meals.sum(:calories) @remaining_protein = current_user.protein_intake - @todays_meals.sum(:protein) @remaining_fats = (current_user.bmr * current_user.fat_percentage / 900).to_i - @todays_meals.sum(:fats) @remaining_carbs = carbs_calculator @fat_grams = current_user.fat_percentage * current_user.bmr / 900 @carb_grams = (carbs_calculator + @todays_meals.sum(:carbohydrates)) end else @no_header = true end end </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload