Tuesday, 15 April 2014

ruby on rails - add unique entry on click -



ruby on rails - add unique entry on click -

i trying add together unique entry on click user can take event have been invited to. user should able click link once. @ nowadays have next code seems adding entry, though dont think best practice, plus unique id doesn't seem working 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 can see query below unique value isn't beingness looked out

select * availabilities 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 comments. validation below makes sure have unique user_id each event_id. can add together multiple scopes if needed.

validates :user_id, uniqueness: { scope: :event_id }

ruby-on-rails orm uniqueidentifier rails-activerecord

No comments:

Post a Comment