How about:  in 'likes' make a unique composite index of both fields 
(created at DB level).  Try to insert a record.  If it fails (because of 
the index) then delete instead.


On Monday, September 10, 2012 11:58:03 PM UTC+1, Mark Li wrote:
>
> I have 3 datatables as follows:
>
>
> db.define_table('songs',
>     Field('songname'),
>     format='%(songname)s')
>
> db.define_table('likes',
>     Field('username', 'reference auth_user'),
>     Field('songname', 'reference songs'))
>
> with the 3rd table being db.auth_user
>
> I would like to implement a "like" button, where clicking it adds an entry 
> to the intermediate table 'likes'. For example, if User1 likes Song1, it 
> would perform the following:
> db.likes.insert(username=auth.user.id, songname=1)
>
>
>
> Right now I have the like button as the following in the view: 
> {{=A('like me', callback=URL('add_like'))}}
>
> and the function as:
> def add_like():
>     db.likes.insert(username=auth.user.id, songname=1)
>
> The problem I'm having is that a user should only be allowed to 'like' a 
> song once; if the user has already liked the song, clicking on 'like' again 
> should remove that like. Basically a toggle functionality for the 'like' 
> button.
>
> I'm not sure how to implement the last part, as it seems checking the 
> entire database for the existence of that record on every 'like' click is 
> overkill. Is there an eloquent way (minimal database calls) to go about 
> implementing this toggle functionality for a 'like' button?
>
>
>
>
>
>

-- 



Reply via email to