0

所以在我的ClientsController.rb我有一个之前的过滤器,它执行以下操作:

检查当前年份和月份。如果月份在六月之后,则将变量设置vote_year为明年。如果不是,则将其设置vote_year为今年。

然后我根据那一年设置日期属性,以及 7 月 1 日的艰难月份和日期。

基本上,这个特定的日期是每年的 7 月 1 日。我希望这个前置过滤器根据运行此过滤器的时间是在 7 月 1 日之前还是之后来设置下一个日期。

我的代码如下:

before_filter :set_next_vote_date, :only => [:create, :new, :edit, :update]

private

def set_next_vote_date
    client = current_user.clients.find(params[:id])
    today = DateTime.parse(Time.now.to_s)
    year = today.year

    if today.month == 7 && today.day == 1
        vote_year = today.year
    elsif today.month > 6
        vote_year = year + 1
    else
        vote_year = year
    end

    client.next_vote = "#{vote_year}-07-01"         
end

问题是,每当我在这些控制器上执行任何这些操作时,都不会引发错误。但是,next_vote客户记录上的属性没有被更新。

我错过了什么?

编辑1:

使用update_attribute(不带!)后,我没有收到错误消息,但我没有在日志中看到此特定属性正在更新。

Started PUT "/clients/1" for 127.0.0.1 at 2012-09-08 20:09:17 -0500
Processing by ClientsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"J172LuZQc5N=", "client"=>{"name"=>"John F Kennedy", "email"=>"jfk@email.com", "phone"=>"8234698765", "firm_id"=>"1", "topic_ids"=>"2"}, "commit"=>"Update Client", "id"=>"1"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Client Load (0.1ms)  SELECT "clients".* FROM "clients" WHERE "clients"."user_id" = 1 AND "clients"."id" = ? LIMIT 1  [["id", "1"]]
   (0.1ms)  begin transaction
   (0.0ms)  commit transaction
  CACHE (0.0ms)  SELECT "clients".* FROM "clients" WHERE "clients"."user_id" = 1 AND "clients"."id" = ? LIMIT 1  [["id", "1"]]
   (0.1ms)  begin transaction
  Topic Load (0.6ms)  SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT 1  [["id", 2]]
  Topic Load (0.2ms)  SELECT "topics".* FROM "topics" INNER JOIN "clients_topics" ON "topics"."id" = "clients_topics"."topic_id" WHERE "clients_topics"."client_id" = 1
   (0.4ms)  UPDATE "clients" SET "phone" = 823498765, "updated_at" = '2012-09-09 01:09:17.631839' WHERE "clients"."id" = 1
   (1.4ms)  commit transaction
Redirected to http://localhost:3000/clients/1

请注意,该next_vote属性未更新。当然,我没有在edit部分表单中包含该属性,但我假设这before_filter- 如果它正在执行,将更新记录。但我什至不确定它是否正在执行。

编辑2:

没关系,它现在似乎正在工作。上面的日志粘贴是在编辑操作之后,而 before_filter 在编辑操作之前执行 - DUH!傻我:)

4

2 回答 2

1

更改属性后,您似乎没有保存客户端。

关于如何稍微清理该代码的一些进一步建议:

before_filter :set_next_vote_date, :only => [:create, :new, :edit, :update]

private

def set_next_vote_date
  client    = current_user.clients.find(params[:id])
  today     = Date.today
  vote_year = today.month > 6 ? today.year + 1 : today.year

  client.update_attribute(:next_vote, Date.new(vote_year, 7, 1))
end
于 2012-09-09T00:46:57.763 回答
1

尝试:

def set_next_vote_date
  client = Client.find(params[:id])
  today = Time.now
  year = today.year

  if today.month == 7 && today.day == 1
    vote_year = year
  elsif today.month > 6
    vote_year = year + 1
  else
    vote_year = year
  end

  client.update_attribute!(:next_vote, "#{vote_year}-07-01")
end

更新属性!(注意爆炸声)如果出现问题,将导致引发异常.. 至少允许您使用 pry-rescue 进行救援并查看发生了什么。不需要调用 save ,因为它将在稍后的 activerecord 回调周期中持续存在。

于 2012-09-09T00:48:01.183 回答