The uniqueness for a field

Rails 3.1.3

Say, I have tables, Video and Script. Their association is

Video:1 — n:Script

Also, Script has a field called ‘value’, (which is decimal, but the type
is not important here).

I want every ‘value’ to be unique for EACH Video. In other words,

Video1 : script-value=1, script-value=2, script-value=4 …

is OK. But NOT

Video1 : script-value=2, script-value=2, script-value=4 …

If I set ‘:value, uniqueness => true’ in Script.rb, a critical problem
occurs.
script-value s are unique for EACH Video, but they are not for all
Videos.

Video1 : script-value:1, script-value:2, script-value:4 …
Video2 : script-value:1, script-value:7, script-value:9 …

Video1 and Video2 has the same script-values (=1), from which
‘uniqueness => true’ setting prevents.

Is there anyway to allow Videos to have the same 'value’s, which however
are distinct from each other among the values that a single Video
possesses?

soichi

Thanks!

validates :value, :uniqueness => { :scope => :video_id }

solves the problem.

soichi

On 18 March 2012 10:52, Soichi I. [email protected] wrote:

Is there anyway to allow Videos to have the same 'value’s, which however
are distinct from each other among the values that a single Video
possesses?

GIYF:
http://stackoverflow.com/questions/5494660/how-to-validate-uniqueness-in-rails-3-model-if-i-want-to-check-if-there-is-a-2-f

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs