First and foremost, thanks to everyone taking time to read through this post. Many, many thanks in advance.
I’m currently building a small registration system for classes. I’m stopping users from registering twice to the same event through the model with “validates :user_id, :uniqueness => {:scope => :occurrence_id}” in my Registration controller. I want to extend this to my views by greying out a “register” button.
I’m thinking that the best way is by writing a method in the occurrence model that goes something like this:
def already_registered?(user)
if user.id == self.registrations.find()
false
else
true
end
end
Now I know for sure that this doesn’t work. How can I search if one occurrence has been has not been register by the current user?
models/registration.rb
class Registration < ActiveRecord::Base
belongs_to :user
belongs_to :occurrence
attr_accessible :occurrence_id, :registration_date, :user_id, :occurrence, :user
validates :user_id, :uniqueness => {:scope => :occurrence_id}
end
models/occurrence.rb
class Occurrence < ActiveRecord::Base
belongs_to :course
belongs_to :location
has_many :registrations
has_many :users, :through => :registrations
validates :price, :presence => true
attr_accessible :course_id, :datetime_end, :datetime_start, :location_id, :price, :teacher_id, :class_size
def remaining_seats
self.class_size - self.registrations.count
end
end
I’m not sure if I really understand your question, but try with
and you call it with