Forum: Ruby on Rails Handling database table dependenciese

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ms (Guest)
on 2009-03-18 13:05
(Received via mailing list)
Hey,

thanks for reading this post. How do you handle dependencies? A really
simple example: I've got a table called "entity" and another table,
let's say, "entity_type". The entity types can be changed, of course,
they also should be deletable, but only, if they are not used by some
entities! How can I ensure this in Rails or directly in the DBMS
(postgres)? Do I have to check it manually within a validate-method in
the model?
Marnen L. (Guest)
on 2009-03-18 18:06
ms wrote:
> Hey,
>
> thanks for reading this post. How do you handle dependencies? A really
> simple example: I've got a table called "entity" and another table,
> let's say, "entity_type". The entity types can be changed, of course,
> they also should be deletable, but only, if they are not used by some
> entities! How can I ensure this in Rails or directly in the DBMS
> (postgres)?

You need foreign key constraints in the database; check the Postgres
docs for more details.  I would *highly* recommend using the
foreign_key_migrations plugin, which will manage these constraints
automatically from your migrations (it's at
http://github.com/harukizaemon , along with a lot of other good stuff).

Big tip: always make your constraints deferrable. It will make testing
*much* easier.

> Do I have to check it manually within a validate-method in
> the model?

I suppose you could, but the DB will already do that if you have the
constraints.

Best,
--
Marnen Laibow-Koser
removed_email_address@domain.invalid
http://www.marnen.org
This topic is locked and can not be replied to.