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