From: niklas | brueckenschlaeger Date: 2011-01-05T00:27:45+09:00 Subject: Re: Validates is exists --=-HtOywMu71+UV+iywQkkF Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 =3D> :event_users end class EventUser belongs_to :event belongs_to :user validates_uniqueness_of :user_id, :scope =3D> :event, :message =3D> "You can only sign up once for a event" end class User has_many :events, :through =3D> :event_users end On Tue, 2011-01-04 at 03:14 +0900, Jose tomas R. wrote: > Event has_and_belongs_to_many :users > User has_and_belongs_to_many :events >=20 > Im trying to verify that user doesnt signup more than one time >=20 >=20 > def signup > @event =3D Event.find(params[:id]) > @user =3D current_user > respond_to do |format| > if @event.users.find(@user) !=3D @user.id > @user.events << @event > format.html { redirect_to(@event, :notice =3D> 'You where > successfully Signed Up.') } > format.xml { head :ok } > else > format.html { redirect_to(@event, :notice =3D> 'You where already > Signed Up.') } > format.xml { head :ok } > end > end > end >=20 > but i think that's no ok, i get this message, when the user havent > signed up: >=20 > Couldn't find User with ID=3D2 [WHERE ("events_users".event_id =3D 7 )] >=20 --=-HtOywMu71+UV+iywQkkF Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iQIbBAABAgAGBQJNIzxrAAoJEIQdQGZOjo827noP91aNY/DZ5Hapbl1Pr+MrzpV6 HppiUMIHuK3tFHiscsrGeumH80IQO3lcsCE4Z/H6mo4QDVXiLU72fwz32PaKDSnd iRpCcFnwupMJa19hHfMH3qHbAcfSA0d4RdvrltYKu/2R73KDYZ2OqPrt48II8aE1 pQaCFDMbcwMwbQsQnSTWCbkHJgVEv7qg4KU2DN5IKQ9QXzxWdnIpibBctLWWiAT+ 8e5ymOW7gyc2qDoUFjPtGxLqJDsGy0ckZl2Xcio4HIpbhHXTHf0qeMnnjUrd9pCp 1eHe3zvI2wQ+6c5bngqvkLa6zBogG6PdtnaRpUnsJUq8mV7v7lwTcXw5F6SlKZ4P L8GvRFw3ujo2TvJnbvZiTUmZ/NYhcYNSCmnFQ0bf5XeO9opeCS+P8F0xCHD0ub6/ jh4VIPB+cGdJ5K9yOJSGymfAG8W/c+JBAx6UC/W70AtNzBXgX4Nuzav5hjN2ITTq EqKAwiODhRTFMwg5xiZmX7+I+fbpr8IryuIma45cOD3Csr8WKMXVAlljWyc+iBfm eptf9D2oc1ULlh+fS8I10DXuxOxIrXh4CrLvUGKQak+vxbGd59hcT/+jgvbhBulC JhMUYQBdZZiR30lX1np+02RKMW82mnaPtkSKVY6xtNp1VjuGJGwI9fg1gs27j8On sfOZa65FyX8WPjjg3zg= =SiD9 -----END PGP SIGNATURE----- --=-HtOywMu71+UV+iywQkkF--