This is my setup. I have a table user where all user with password and so on are set. The customer table has some customer related data. The user_id is the foreign key to relate the customers to the user. On of the column in the customer table is the customernumber. These numbers has to be uniq for each user_id. class Customers> belongs_to :user > end > > class Users > has_many :customers > endFor example. customer | customernumber | user_id 1 0001 1 2 0002 1 3 0001 2 4 0001 3 5 0002 3 How can i approach this? Any hints what I have to look for? thanks in advance best regards denym -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/351e0d0d-a672-4a7e-95e4-43db470e90e0%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
On Sat, May 18, 2013 at 8:14 AM, Denny Mueller <macdankk-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> This is my setup. I have a table user where all user with password and so on > are set. The customer table has some customer related data. The user_id is > the foreign key to relate the customers to the user. > > On of the column in the customer table is the customernumber. These numbers > has to be uniq for each user_id.Is this a legacy database you''re trying to use with Rails? It doesn''t appear to follow Rails conventions. In this example, is "customer" a unique auto-generated identifier? If so, why do you need "customernumber" to also be unique? If not, what is it?> customer | customernumber | user_id > 1 0001 1 > 2 0002 1 > 3 0001 2 > 4 0001 3 > 5 0002 3-- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCvXDs86nz5%2BYgrpaaDqEbOvVRLcWx%3DQbioNhyeJPh7pg%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
On Saturday, 18 May 2013 11:14:35 UTC-4, Denny Mueller wrote:> > This is my setup. I have a table user where all user with password and so > on are set. The customer table has some customer related data. The user_id > is the foreign key to relate the customers to the user. > > On of the column in the customer table is the customernumber. These > numbers has to be uniq for each user_id. > > > class Customers >> belongs_to :user >> end >> >> class Users >> has_many :customers >> end > > > For example. > > customer | customernumber | user_id > 1 0001 1 > 2 0002 1 > 3 0001 2 > 4 0001 3 > 5 0002 3 > > > How can i approach this? Any hints what I have to look for? >validates_uniqueness_of has a ''scope'' option that will do exactly what you''re describing: class Customer < ActiveRecord::Base belongs_to :user validates_uniqueness_of :customernumber, :scope => :user_id end Depending on what you''re intending to use the customer number for, you may also want to take a look at something like acts_as_list. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ddd230c8-db99-40bd-ae18-93145083cb0f%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Matt Jones wrote in post #1109607:> On Saturday, 18 May 2013 11:14:35 UTC-4, Denny Mueller wrote: >>> belongs_to :user >> 1 0001 1 >> 2 0002 1 >> 3 0001 2 >> 4 0001 3 >> 5 0002 3 >> >> >> How can i approach this? Any hints what I have to look for? >> > > validates_uniqueness_of has a ''scope'' option that will do exactly what > you''re describing:Also note that any use of validates_uniqueness_of should also be backed by unique constraints in the database to prevent race conditions from causing duplicates. For more details on race conditions related to uniqueness validations read the section under "Concurrency and integrity" here: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_uniqueness_of -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/46b921321ce9e4d0cb821f7270560fe9%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.