Hi,
In my app. I have the following set of models
class Course < ActiveRecord::Base
has_many :registrations
has_many :users, :through => :registrations
end
class User < ActiveRecord::Base
has_many :registrations
has_many :courses, :through => :registrations
end
class Registration < ActiveRecord::Base
belongs_to :user
belongs_to :course
end
each course model has a 'capacity' attribute, which dictates how many
places are available on that particular course. This is how I enforce
the 'capacity' constraint when a registration is created:
class Course < ActiveRecord::Base
...
def enrol(user)
if registration_count >= capacity
errors.add_to_base("this course is now full")
return false
end
self.registrations.create(:user_id => user.id)
end
def registration_count
registrations.length
end
...
end
My concern is that my code won't guard against a race condition where
2 or more users try to register for a course that is near it capacity
limit. My first thought was to wrap the registration code in a
transaction and rollback if the capacity of registrations on a course
has been exceeded.
I'm not sure if this is a valid approach and I would love to hear from
someone who could provide some adivice on how I might proceed.
thanks and regards,
C.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---