Hi, I´m new in Rails framework, and i´m trying to create my first app. I create two tables: clients --id --name --phone keys --id --client_id --value I use migration to create the relationship on tables: class AddRelationship < ActiveRecord::Migration def self.up change_table :keys do |t| t.belongs_to :client end end def self.down change_table :keys do |t| t.remove_belongs_to :client end end end but, when I insert a key without a client_id, this is created successfully, but should return an error. Did I''m doing something wrong? Ps: sorry about my poor english :( --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
> > but, when I insert a key without a client_id, this is created > successfully, but should return an error. > Did I''m doing something wrong? >Rails does not by default enforce referential integrity for your models. You can add the necessary indexes and settings to enforce this referential integrity in your database layer, or you could put validation in your Key model to enforce the presence of a client_id, or you could do both. You could also create your keys using a construct like the following to ensure the keys are populated correctly: client = Client.find(1) client.keys << Key.new(blah blah blah) -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Cayce...I''ll put validation on database. On 2 jun, 17:36, Cayce Balara <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > but, when I insert a key without a client_id, this is created > > successfully, but should return an error. > > Did I''m doing something wrong? > > Rails does not by default enforce referential integrity for your models. > You can add the necessary indexes and settings to enforce this > referential integrity in your database layer, or you could put > validation in your Key model to enforce the presence of a client_id, or > you could do both. > > You could also create your keys using a construct like the following to > ensure the keys are populated correctly: > > client = Client.find(1) > client.keys << Key.new(blah blah blah) > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---