Depot Application Tutorial and Foreign Keys

Your associations, validations, and callbacks emulate foreign keys so there’s no reason to define them in terms of data constraints.

class Foo < ActiveRecord::Base belongs_to :bar validates_presence_of :foo_id

validates_associated :foo

end

class Bar < ActiveRecord::Base has_many :foos, :dependent => :destroy end

You can add indexes via a migration.

However, one thing that foreign keys give you is an index on that column. So if you don’t declare them as foreign keys (which I don’t) then you should declare an index on those columns to help with lookups.

Of course. But you get into that sticky situation of moving business logic into your database. This is one reason SOA is becoming so popular… make application A use application B to control the database rather than going to it directly.

You’ll touch off a holy war here if you’re not careful :slight_smile: The main point is that Rails does not need nor require them, and you may get unexpected results if, for example, your database has constraints of the foreign key that restricts its input but you have not handled that in the models, or perhaps you cascade-delete records on your database and you don’t deal with that in your apps.

In short, it’s all up to you. There are ways to handle all of those concerns; if you need to use FKs, by all means do so.