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

时间:2016-02-18 01:30:58

标签: ruby-on-rails rails-activerecord rails-models

我正在尝试编写一个范围或方法,其中我获取实例(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

1 个答案:

答案 0 :(得分:0)

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

应该有用......

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

当您执行sql-snippets时,您需要在SQL中引用表的名称,而不是将其视为单个rails对象的名称。这意味着始终使用复数版本:))