How to handle massive storage of records in database for user authorization purposes?

后端 未结 7 1838
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 09:30

I am using Ruby on Rails 3.2.2 and MySQL. I would like to know if it is \"advisable\" / \"desirable\" to store in a database table related to a class all records related to

7条回答
  •  忘掉有多难
    2021-01-05 10:01

    First of all, it is good to think about default values and behaviors and not store them in the database. For example, if by default, a user cannot read an article unless specified, then, it does not have to be stored as false in the database.

    My second thought is that you could have a users_authorizations column in your articles table and a articles_authorizations in your users table. Those 2 columns would store user ids and article ids in the form 3,7,65,78,29,78. For the articles table for example, this would mean users with ids 3,7,65,78,29,78 can access the articles. Then you would have to modify your queries to retrieve users that way:

    @article = Article.find(34)
    @users = User.find(@article.user_authorizations.split(','))
    

    Each time an article and a user is saved or destroyed, you would have to create callbacks to update the authorization columns.

    class User < ActiveRecord 
       after_save :update_articles_authorizations
       def update_articles_authorizations
         #...
       end
    end
    

    Do the same for Article model.

    Last thing: if you have different types of authorizations, don't hesitate creating more columns like user_edit_authorization.

    With these combined techniques, the quantity of data and hits to the DB are minimal.

提交回复
热议问题