为什么这个before_filter没有执行?

时间:2012-09-09 00:29:18

标签: ruby-on-rails ruby-on-rails-3 before-filter

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

检查当前年份和月份。如果月份在6月之后,则将变量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!傻我:)。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

update_attribute! (注意爆炸)如果出现问题,将会引发异常..至少允许你用撬救营救,看看发生了什么。不需要调用save,因为它将在稍后的activerecord回调周期中保留。

相关问题