Validates is exists

Event has_and_belongs_to_many :users
User has_and_belongs_to_many :events

Im trying to verify that user doesnt signup more than one time

def signup
@event = Event.find(params[:id])
@user = current_user
respond_to do |format|
if @event.users.find(@user) != @user.id
@user.events << @event
format.html { redirect_to(@event, :notice => ‘You where
successfully Signed Up.’) }
format.xml { head :ok }
else
format.html { redirect_to(@event, :notice => ‘You where already
Signed Up.’) }
format.xml { head :ok }
end
end
end

but i think that’s no ok, i get this message, when the user havent
signed up:

Couldn’t find User with ID=2 [WHERE (“events_users”.event_id = 7 )]

On Mon, Jan 3, 2011 at 7:14 PM, Jose tomas R. [email protected]
wrote:

Event has_and_belongs_to_many :users
User has_and_belongs_to_many :events

Im trying to verify that user doesnt signup more than one time

  1. Rails questions get better service one link over in the Rails forum.
    :slight_smile:

  2. Given that there’s dozens of free email services out there and
    making up a fake name is trivial, how do you suppose to stop the user
    signing up multiple times, anyway? Short of using credit card numbers,
    and I doubt you can verify those with the issuers, anyway (PCI
    compliance is not something you want to burden yourself with).


Phillip G.

Though the folk I have met,
(Ah, how soon!) they forget
When I’ve moved on to some other place,
There may be one or two,
When I’ve played and passed through,
Who’ll remember my song or my face.

On Mon, Jan 3, 2011 at 12:38 PM, Phillip G. <
[email protected]> wrote:

  1. Given that there’s dozens of free email services out there and
    making up a fake name is trivial, how do you suppose to stop the user
    signing up multiple times, anyway? Short of using credit card numbers,
    and I doubt you can verify those with the issuers, anyway (PCI
    compliance is not something you want to burden yourself with).

Of course, if the site lags a little bit, and they impatiently click the
button five times, you don’t want to displace four other guests.

On Mon, Jan 3, 2011 at 8:11 PM, Josh C. [email protected] wrote:

Of course, if the site lags a little bit, and they impatiently click the
button five times, you don’t want to displace four other guests.

It’s a reasonable assumption that a person registering for an event 5
times in quick succession meant to only go once. It’s also a nice UX
trick to use JavaScript to grey out the form’s submit button once it
has been clicked. Or you go to a placeholder site while you process
the data. Or send the data off to a worker threat, and redirect the
user to another page, telling them their request is being processed
and will show up soon.


Phillip G.

Though the folk I have met,
(Ah, how soon!) they forget
When I’ve moved on to some other place,
There may be one or two,
When I’ve played and passed through,
Who’ll remember my song or my face.

Jose tomas R. wrote in post #972073:

Event has_and_belongs_to_many :users
User has_and_belongs_to_many :events

Im trying to verify that user doesnt signup more than one time

if @event.users.find(@user) != @user.id

Foo.find(x) looks for a record with id X, and will raise an exception if
it doesn’t exist, as you already discovered.

Completely untested, but you could try some variation of

if @event.users.find_by_id(@user.id)

if @event.users.where(:id => @user.id).count > 0

Validations should happen in the model, not the controller. You could
use has_many :through, which gives you more control over the join table:

class Event
has_many :users, :through => :event_users
end

class EventUser
belongs_to :event
belongs_to :user
validates_uniqueness_of :user_id, :scope => :event, :message => “You
can only sign up once for a event”
end

class User
has_many :events, :through => :event_users
end

thanks for that, worked fine