【发布时间】:2013-04-04 10:53:27
【问题描述】:
我选择
m_repeats = Event.where(:repeat => 'monthly').where("schedule < ?", date.end_of_month)
然后我需要更改每个元素中的 shedule(它是日期字段)。
我尝试做:
m_repeats.map{ |elem| elem.schedule.year = date.today.year, elem.schedule.month = date.today.month }
但我得到了错误:
Mysql2::Error: Unknown column 'schedule' in 'where clause': SELECT `events`.* FROM `events` WHERE `events`.`repeat` = 'monthly' AND (schedule < '2013-04-30')
或 #
的未定义方法“计划”什么是正确的方法?
【问题讨论】:
-
您确认events表在schema.rb中有schedule字段了吗?可以访问 Event.first.schedule 吗?
-
是的。 create_table "事件", :force => true do |t| t.string "title" t.date "shedule" t.integer "user_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.string "repeat" end 还有我的“Where(...)”选择工作正常,我只在添加映射代码时看到错误
-
是的,但
map不是您收到数据库错误的原因,看起来像第二个where。 -
我认为 Frans 是正确的 -- 您在
map看到了错误,因为 ActiveRecord 在您询问结果之前不会执行查询
标签: ruby-on-rails arrays activerecord map