i am trying to add a unique entry on click so that a user can accept an event they have been invited to. the user should only be able to click this link once. at present i have the following code which seems to be adding the entry, though i dont think its the best practice, plus the unique id doesn’t seem to be working too well
haml
%li= link_to 'Add', availabilities_path(:availability => {:team_id => @event.team_id, :user_id => user, :event_id => @event.id, :unique_id => availability_unique_id(user,@event) }), :remote => true, :method => :post, :class => 'button tiny success'
model
validates :unique_id, :presence => true, :uniqueness => true
unique method
def availability_unique_id(player,schedule)
Base64.encode64("#{player.to_s}_#{schedule.id.to_s}_#{schedule.team.id.to_s}")
end
as you can see from the query below the unique value isn’t being looked out
select * from availabilities where user_id = 41;
id | available | user_id | team_id | event_id | created_at | updated_at | unique_id | comment
----+-----------+---------+---------+----------+----------------------------+----------------------------+----------------------------------+---------
61 | | 41 | | | 2012-11-04 13:48:22.794214 | 2012-11-04 13:48:22.794214 | NDFfNDNfMQ== +|
| | | | | | | |
84 | | 41 | 1 | 75 | 2013-02-09 14:03:29.792374 | 2013-02-09 14:03:29.792374 | IzxVc2VyOjB4YWY3NTMxND5fNzVfMQ==+|
| | | | | | | |
85 | | 41 | 1 | 75 | 2013-02-09 14:06:04.131862 | 2013-02-09 14:06:04.131862 | IzxVc2VyOjB4YjJhODBiOD5fNzVfMQ==+|
| | | | | | | |
87 | | 41 | 1 | 75 | 2013-02-09 14:07:31.77788 | 2013-02-09 14:07:31.77788 | IzxVc2VyOjB4YjBhYjdiMD5fNzVfMQ==+|
| | | | | | | |
from the comments. the validation below makes sure that you have a unique
user_idfor eachevent_id. you can add multiple scopes if needed.