Onur Özgür ÖZKAN
2011-Nov-11 10:25 UTC
belongs_to :model validation with mandatory foreign keys and foreign key at DB layer
Hi Folks, *First Question* I want to learn that there is a native way make building normal and mandatory belongs_to associations. Let give a simple example, I have two models, first one is Tenant, second is User. User belongs to Tenant which mean there is a User.tenant_id. I can create, save or update User Model without User.tenant_id . I named this is normal belongs_to . What i want is User model can''t update or save if tenant_id = nil. What is the best solution for this problem. My schema is https://github.com/kebab-project/server-ror/blob/master/db/schema.rb My models are https://github.com/kebab-project/server-ror/tree/master/app/models My migrations are https://github.com/kebab-project/server-ror/tree/master/db/migrate *Note : *User Model extends from TenantScope model! *Second Question* * * I see that rails doesn''t add foreign key at DB layer. There are several gems for this issue. But i can''t understand why we don''t add foreign key. Why this is a good idea? Is it over performance or portability? Best Regards. -- *Onur Özgür ÖZKAN* ***lab2023 - internet teknolojileri* www.lab2023.com *Adres:* Pamukkale Teknoloji Geliştirme Bölgesi İdari Bina B-Blok Z13 Kınıklı / Denizli / TÜRKİYE *Telefon:* +90 258 215 50 10 - *E-posta:* info-QnCSCsRl1hBBDgjK7y7TUQ@public.gmane.org -- 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.
Colin Law
2011-Nov-11 10:37 UTC
Re: belongs_to :model validation with mandatory foreign keys and foreign key at DB layer
2011/11/11 Onur Özgür ÖZKAN <onur.ozgur.ozkan-QnCSCsRl1hBBDgjK7y7TUQ@public.gmane.org>:> Hi Folks, > First Question > > I want to learn that there is a native way make building normal and > mandatory belongs_to associations. Let give a simple example, I have two > models, first one is Tenant, second is User. User belongs to Tenant which > mean there is a User.tenant_id. I can create, save or update User Model > without User.tenant_id . I named this is normal belongs_to . What i want is > User model can''t update or save if tenant_id = nil.Use a validation in the User model. See the Rails Guide on Active Record Validations and Callbacks. Colin -- 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.
Frederick Cheung
2011-Nov-11 11:21 UTC
Re: belongs_to :model validation with mandatory foreign keys and foreign key at DB layer
On Nov 11, 10:25 am, Onur Özgür ÖZKAN <onur.ozgur.oz...-QnCSCsRl1hBBDgjK7y7TUQ@public.gmane.org> wrote:> Hi Folks, > > *First Question* > > I want to learn that there is a native way make building normal and > mandatory belongs_to associations. Let give a simple example, I have two > models, first one is Tenant, second is User. User belongs to Tenant which > mean there is a User.tenant_id. I can create, save or update User Model > without User.tenant_id . I named this is normal belongs_to . What i want is > User model can''t update or save if tenant_id = nil.When you add the column, make it a not null column.> * > I see that rails doesn''t add foreign key at DB layer. There are several > gems for this issue. But i can''t understand why we don''t add foreign key. > Why this is a good idea? Is it over performance or portability? >There is a school of thought that says that the database shouldn''t have any application/business logic. Personally I always use foreign keys, unique indexes etc - only the database itself can give me a cast iron guarantee for those things - rails validations are subject to race conditions in some cases (and of course don''t help you if you ever manipulate data outside of rails) 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.