小编Cat*_*hal的帖子

如何在Rails应用程序中避免竞争条件?

我有一个非常简单的Rails应用程序,允许用户在一组课程中注册他们的出勤.ActiveRecord模型如下:

class Course < ActiveRecord::Base
  has_many :scheduled_runs
  ...
end

class ScheduledRun < ActiveRecord::Base
  belongs_to :course
  has_many :attendances
  has_many :attendees, :through => :attendances
  ...
end

class Attendance < ActiveRecord::Base
  belongs_to :user
  belongs_to :scheduled_run, :counter_cache => true
  ...
end

class User < ActiveRecord::Base
  has_many :attendances
  has_many :registered_courses, :through => :attendances, :source => :scheduled_run
end
Run Code Online (Sandbox Code Playgroud)

ScheduledRun实例具有有限数量的可用位置,一旦达到限制,就不能再接受更多的考勤.

def full?
  attendances_count == capacity
end
Run Code Online (Sandbox Code Playgroud)

attendances_count是一个计数器缓存列,包含为特定ScheduledRun记录创建的出勤关联数.

我的问题是,当一个或多个人同时尝试在课程中注册最后一个可用位置时,我不完全知道确保不会发生竞争条件的正确方法.

我的考勤控制器如下所示:

class AttendancesController < ApplicationController
  before_filter :load_scheduled_run
  before_filter :load_user, :only => :create

  def new
    @user = User.new
  end

  def …
Run Code Online (Sandbox Code Playgroud)

database activerecord ruby-on-rails optimistic-locking

20
推荐指数
1
解决办法
7617
查看次数

TextMate将正则表达式搜索的结果保存到新窗口中

我希望能够在TextMate中执行搜索,但将生成的匹配复制到单独的文件中.

我正在使用以下表达式:

(?<=\()(.*?)(?=\))

匹配嵌入在一行文本的括号内的电子邮件地址,如下所示:

A N Other (another@example.com)

我正在使用的文件有几百个条目,全部由CR-LF(\n)分隔.

我希望能够仅将文本的电子邮件片段提取到新文件中以供进一步处理.但是,TextMate中的搜索对话框似乎只支持替换匹配的文本.我只是想知道是否有办法实现这一目标.

textmate

1
推荐指数
1
解决办法
1177
查看次数