Rails 4 : Access attributes from model -
i have model reservation 2 columns, user_id
, teleporter_id
.
i want lock creation of reservation 3 same teleporter_id
don't know how access attribute teleporter_id
model i'm creation.
here code :
class reservation < activerecord::base # relations belongs_to :teleporter belongs_to :user # validations validate :max_reservation def max_reservation if reservation.where(:teleporter_id => self.teleporter_id).count >= 3 errors.add(:base, t('reservation.model.error.max_reservation')) end end end
i think problem self.teleporter_id
don't know how access attribut teleporter_id current model.
try this:
def max_reservation _id = self.teleporter_id errors.add(:base, t('reservation.model.error.max_reservation')) unless reservation.where(teleporter_id: _id).count <= 3 end
Comments
Post a Comment