ruby on rails - validates :name, uniqueness: {scope: user_id} -


i have added validation 1 model:

validates :name, uniqueness: {scope: user_id} 

and added add_index on migration:

add_index(:posts, :name) 

but read on rails api page part data integrity.

and wondering if have integrity errors on models, question is: should rewrite indexes as?

add_index(:posts, [:name, :user_id]), unique: true

thanks all,

the data integrity you're talking can enforced @ 2 levels know: @ application level , @ database level.

at application level: validation added. at database level: index suggested

you set first one. so, long goes through rails model saved in db, won't have db integrity issue.

however, if other third-party applications may write db, not bad idea enforce uniqueness @ db level.

and if first 1 sufficient, not bad idea neither set second.

in addition, if happen query name associated user_id, better use add_index(:posts, [:name, :user_id]), making queries bit faster.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -