ruby - Rails article voting restriction in model -


i'm building test application in rails, it's news story voting system teach myself rails , ruby.

i'm trying restrict voting user can 'vote up' article once on each article. after research best way i've found of getting work adding validation on vote model i've create checks duplicate vote entry user.

i'm not after code example advice best way of breaking problem down? active record validates work doesn't feel practice put strain @ database level, maybe fine?

hope makes sense, reading.

this in fact right design. in relational setup, you'll have stories, users, , votes, votes have both user_id , story_id. enforcing data integrity database constraint, , can't know if user has voted on story not without going database check. keep kind of higher-level cache prevent quite database access if needed, doing premature optimization. cross bridge when need to.

one thing consider create compound unique index of (user_id, story_id) on votes table. have benefits of backstopping validation , improving vote lookup speed o(log n) operation, should eliminate of time burden. else can disallow votes on old stories, , purge votes records older age (corresponding time @ disallow voting), keep votes table @ relatively constant size on time, rather permitting grow forever.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -