如何将星期几添加到事件表中?一周中的几天需要单独的表格吗?

时间:2019-01-28 22:19:24

标签: sql ruby-on-rails activerecord

(这是用于副项目/个人网站)

现在我有带有以下各列的活动表/模型... 如何添加事件发生在一周中的特定日期? 用户应该能够从视图中单击星期几,如果选择了该日期,则该天的值将变为true。

所以我当时想我需要一个称为星期几的布尔值表,但是我不太确定如何将其与活动模型绑定。我还想我可以在活动表中设置7天布尔值。我觉得那真的很草率

活动模型/表格

create_table "activities", force: :cascade do |t|
    t.text "content", null: false
    t.string "activity_name", null: false
    t.string "additional_info"
    t.bigint "user_id"
    t.string "cost", null: false
    t.string "addressLN1", null: false
    t.string "addressLN2"
    t.string "city", null: false
    t.string "state", null: false
    t.string "zip", null: false
    t.string "picture"
    t.string "capacity", null: false
    t.string "contact_number"
    t.string "contact_email", null: false
    t.datetime "start_date", null: false
    t.datetime "end_date", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id", "created_at"], name: 
    "index_activities_on_user_id_and_created_at"
    t.index ["user_id"], name: "index_activities_on_user_id"
  end

1 个答案:

答案 0 :(得分:0)

为什么不只存储日期戳,而是使用Date的内置功能来获取星期几?

2.0.0p247 :139 > Date.today
 => Sun, 10 Nov 2013 
2.0.0p247 :140 > Date.today.strftime("%A")
 => "Sunday"