【问题标题】:How can I compare an attribute of a instance, which is a date, to the current date?如何将实例的属性(即日期)与当前日期进行比较?
【发布时间】:2016-02-18 01:30:57
【问题描述】:

我正在尝试编写一个范围或方法,在其中获取实例 (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 &gt;= ?", 7.days.ago)} 这应该可以工作......你得到了什么?您是否检查过&gt;= 是否正确?哦,还有:你的表是 line_item 还是 line_items(记住,在执行 sql-sn-ps 时,你指的是表名,而不是单个 rails 对象的名称)
  • ...@TarynEast 打败了我。在我的回答中,我在不知不觉中几乎重复了您评论的后半部分。如果这解决了问题,并且如果你想创建一个答案,我会记下我的 - 看起来你首先发布了评论。告诉我。

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


【解决方案1】:

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

应该可以...

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

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

【讨论】:

  • 非常感谢!这让我发疯了。我不得不做一两个小改动。首先,它计算范围运行的实际日期,因此我不得不将其更改为“8.days.ago”。最后,datetimedate 之间发生了小冲突,所以我不得不在末尾附加 to_date。新范围是:scope :last_week, lambda {where("line_item.last_eaten &gt;= ?", 8.days.ago.to_date)}
猜你喜欢
  • 2017-05-15
  • 1970-01-01
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多