Hi All, Here''s my situation. People have Availability for Events. I need to ensure that each Person is Available for each Event exactly once. I''m thinking the way to do it would be with a before_save method in Availability that does an Availability.find("event_id = ? and person_id = ?", record.event_id, record.person_id) and complains if it finds one. Is this the way to go or is there a better approach? **Leigh ============ #Person.rb class Person < ActiveRecord::Base has_many :availabilities has_many :events, :through => :availabilities default_scope :order => ''name ASC'' validates_presence_of :name, :on => :create, :message => "-- You need to enter your name" end #Availability.rb class Availability < ActiveRecord::Base belongs_to :event belongs_to :person validates_presence_of :person_id, :on => :create, :message => "can''t be blank" validates_presence_of :event_id, :on => :create, :message => "can''t be blank" end #Event.rb class Event < ActiveRecord::Base has_many :availabilities has_many :people, :through => :availabilities default_scope :order => ''date ASC'' validates_presence_of :name, :on => :create, :message => "can''t be blank" validates_presence_of :date, :on => :create, :message => "can''t be blank" end # Schema.rb ActiveRecord::Schema.define(:version => 20110809224443) do create_table "availabilities", :force => true do |t| t.integer "event_id" t.integer "person_id" . . . end create_table "events", :force => true do |t| t.string "name" t.datetime "date" . . . end create_table "people", :force => true do |t| t.string "name" . . . end end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 10, 8:42 pm, Leigh Daniels <leighdaniel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi All, > > Here''s my situation. People have Availability for Events. I need to ensure that each Person is Available for each Event exactly once. > > I''m thinking the way to do it would be with a before_save method in Availability that does an > > Availability.find("event_id = ? and person_id = ?", record.event_id, record.person_id) > > and complains if it finds one. > > Is this the way to go or is there a better approach?I''d stick a unique index on that pair of columns too - it''s the only way to get a cast iron guarantee for this sort of thing. Fred> > **Leigh > > ============> > #Person.rb > class Person < ActiveRecord::Base > has_many :availabilities > has_many :events, :through => :availabilities > default_scope :order => ''name ASC'' > validates_presence_of :name, :on => :create, :message => "-- You need to enter your name" > end > > #Availability.rb > class Availability < ActiveRecord::Base > belongs_to :event > belongs_to :person > validates_presence_of :person_id, :on => :create, :message => "can''t be blank" > validates_presence_of :event_id, :on => :create, :message => "can''t be blank" > end > > #Event.rb > class Event < ActiveRecord::Base > has_many :availabilities > has_many :people, :through => :availabilities > > default_scope :order => ''date ASC'' > validates_presence_of :name, :on => :create, :message => "can''t be blank" > validates_presence_of :date, :on => :create, :message => "can''t be blank" > end > > # Schema.rb > ActiveRecord::Schema.define(:version => 20110809224443) do > create_table "availabilities", :force => true do |t| > t.integer "event_id" > t.integer "person_id" > . . . > end > > create_table "events", :force => true do |t| > t.string "name" > t.datetime "date" > . . . > end > > create_table "people", :force => true do |t| > t.string "name" > . . . > end > end-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > >On Aug 10, 8:42 pm, Leigh Daniels <leighdaniel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi All, >> >> Here''s my situation. People have Availability for Events. I need to >ensure that each Person is Available for each Event exactly once. >> >> I''m thinking the way to do it would be with a before_save method in >Availability that does an >> >> Availability.find("event_id = ? and person_id = ?", record.event_id, >record.person_id) >> >> and complains if it finds one. >> >> Is this the way to go or is there a better approach? > >I''d stick a unique index on that pair of columns too - it''s the only >way to get a cast iron guarantee for this sort of thing. >Is this what you meant, Fred? add_index "availabilities", ["person_id", "event_id"], :name => "person_id_event_id", :unique => true **Leigh>Fred >> >> **Leigh >> >> ============>> >> #Person.rb >> class Person < ActiveRecord::Base >> has_many :availabilities >> has_many :events, :through => :availabilities >> default_scope :order => ''name ASC'' >> validates_presence_of :name, :on => :create, :message => "-- You >need to enter your name" >> end >> >> #Availability.rb >> class Availability < ActiveRecord::Base >> belongs_to :event >> belongs_to :person >> validates_presence_of :person_id, :on => :create, :message => "can''t >be blank" >> validates_presence_of :event_id, :on => :create, :message => "can''t >be blank" >> end >> >> #Event.rb >> class Event < ActiveRecord::Base >> has_many :availabilities >> has_many :people, :through => :availabilities >> >> default_scope :order => ''date ASC'' >> validates_presence_of :name, :on => :create, :message => "can''t be blank" >> validates_presence_of :date, :on => :create, :message => "can''t be blank" >> end >> >> # Schema.rb >> ActiveRecord::Schema.define(:version => 20110809224443) do >> create_table "availabilities", :force => true do |t| >> t.integer "event_id" >> t.integer "person_id" >> . . . >> end >> >> create_table "events", :force => true do |t| >> t.string "name" >> t.datetime "date" >> . . . >> end >> >> create_table "people", :force => true do |t| >> t.string "name" >> . . . >> end >> end > >-- >You received this message because you are subscribed to the Google >Groups "Ruby on Rails: Talk" group. >To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >To unsubscribe from this group, send email to rubyonrails-talk >+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >For more options, visit this group at http://groups.google.com/group/ >rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 10, 9:23 pm, Leigh Daniels <leighdaniel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> >On Aug 10, 8:42 pm, Leigh Daniels <leighdaniel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hi All, > > >> Here''s my situation. People have Availability for Events. I need to > >ensure that each Person is Available for each Event exactly once. > > >> I''m thinking the way to do it would be with a before_save method in > >Availability that does an > > >> Availability.find("event_id = ? and person_id = ?", record.event_id, > >record.person_id) > > >> and complains if it finds one. > > >> Is this the way to go or is there a better approach? > > >I''d stick a unique index on that pair of columns too - it''s the only > >way to get a cast iron guarantee for this sort of thing. > > Is this what you meant, Fred? > > add_index "availabilities", ["person_id", "event_id"], > :name => "person_id_event_id", :unique => true >That''s the one. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.