如何将实例的属性(日期)与当前日期进行比较?

How can I compare an attribute of a instance, which is a date, to the current date?

我正在尝试编写一个范围或方法,在其中获取实例 (line_item) 的属性 (last_eaten) 并将其与当前日期进行比较。如果 last_eaten 的日期是 1-7 天前,它会被放入一个名为 last_week 的数组中。如果 last_eaten 的日期是 8-14 天前,它会被放入一个名为 2_weeks_ago 的数组中。

我已经尝试了很多事情,正如您在注释掉的代码和我已经删除的一些事情中看到的那样,但我无法使任何事情起作用。我是 rails 的新手,如有任何帮助,我们将不胜感激。

型号

  class LineItem < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :recipe_collection


  #scope :last_week, lambda {where("line_item.last_eaten >= ?", 7.days.ago)}
  #scope :last_week, lambda { |weeks| where("last_eaten > ?", weeks) }
  #scope :three_weeks, lambda { where( @line_item.last_eaten < 21.days.ago.to_date) }
  #@line_item = LineItem.where(last_eaten: params[:last_eaten]) -- returns nil
  #@line_item = LineItem.where(last_eaten: params[:last_eaten] < 21.days.ago.to_date)

  #def menu
  # list = []
  # if LineItem.last_eaten.day.to_i > 21.days.ago.day.to_i
  #     LineItem.last_eaten.each do |recipe_id|
  #         LineItem.recipe_id  << list
  #     end
  # end
  # list
  #end

end

架构

ActiveRecord::Schema.define(version: 20151229223926) do

  create_table "directions", force: :cascade do |t|
    t.text     "step"
    t.integer  "recipe_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "directions", ["recipe_id"], name: "index_directions_on_recipe_id"

  create_table "ingredients", force: :cascade do |t|
    t.string   "name"
    t.integer  "recipe_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "ingredients", ["recipe_id"], name: "index_ingredients_on_recipe_id"

  create_table "line_items", force: :cascade do |t|
    t.integer  "recipe_id"
    t.integer  "recipe_collection_id"
    t.datetime "created_at",           null: false
    t.datetime "updated_at",           null: false
    t.date     "last_eaten"
  end

  add_index "line_items", ["recipe_collection_id"], name: "index_line_items_on_recipe_collection_id"
  add_index "line_items", ["recipe_id"], name: "index_line_items_on_recipe_id"

  create_table "recipe_collections", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "recipes", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.integer  "user_id"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

scope :last_week, lambda {where("line_item.last_eaten >= ?", 7.days.ago)}

应该可以...

但进一步阅读让我意识到你的 table 被称为 line_items,而不是 line_item

当你做sql片段时,你需要参考SQL中table的名字,而不是像对待一个个体rails 对象的名称。这意味着始终使用复数形式:)