Ruby on Rails
Monday, May 20, 2013
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 Customersbelongs_to :user
endclass Usershas_many :customers
endFor example.customer | customernumber | user_id1 0001 12 0002 13 0001 24 0001 35 0002 3How 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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment