简体   繁体   中英

ruby on rails pairing users in database

How can I write a code in Ruby on rails that picks user X from my database( let say I have a column called 'taskdone' which accepts only boolean values ... And user X is true in such column) such that when a new user signs up, user X is displayed on his profile. And the new user is given a task to complete. If he completes it successfully user X can confirm from his own account that the new user has completed his task by clicking a button which writes a value of true to the new users paid 'task done ' column. And after user X has confirmed four people, his value in the column should go back to false. It's quite complicated please I need someone to help am frustrated searching Google without answers

You can do this in two ways

1- If you need the information about "which user confirms another user" You can create an association to hold for each user X which other users he has confirmed.

2 - If you won't use these info you can just create a column that counts the number of confirmed users in the Users tabel.

In both cases, After a user confirms another user make a check if he has confirmed 4 users or not using the callback after create and then update the taskdone column.

after_create :update_task_done

In the first way do

def update_task_done
    if self.confirmed_users.count == 4
        self.update(taskdone: false)
    end
end

In the second way

def update_task_done
    if self.confirmed_users_count == 4 # notice this is column.
        self.update(taskdone: false)
    end
end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM