Forum: Ruby on Rails referential integrity

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.
4cc9992c28e7d8f16ea9ad5eda4dae0f?d=identicon&s=25 doughie (Guest)
on 2009-04-14 00:54
(Received via mailing list)
I am starting out in rails and am loving it! Thanks to all the
contributors!

I made a web app that had about 7 models, with some referring to each
other, eg a team had a member and so on.

This relationship was not enforced in the model or in the database, it
was kind of hacked together in the view by creating html select
inputs.

I found very quickly that deleting models and adding new ones meant
that objects that should not be related were.

I thought at first I should implement the correct cascading deletes.
I have looked into this a little more and have now redefined all my
models correctly, eg a team has_many :members.

I looked at the mysql database this created and there are no foreign
key relationships.

question 1) should there be foreign key relations in mysql db?  I used
the script/generate model and then rake db:migrate

my model:
class Developer < ActiveRecord::Base
  belongs_to :team
end

question 2) in my conceptual model my developer belongs to a team.  So
i have the model above.  Should there be a relationship in team
describing how the link exists to developer? Currently mine is empty:
class Team < ActiveRecord::Base
end

thanks in advanced for any help!
548dde5574b950a8ac2f09cb0ee681d5?d=identicon&s=25 Andrew France (Guest)
on 2009-04-14 01:44
(Received via mailing list)
On Apr 13, 11:06 pm, doughie <eamonn.fahe...@gmail.com> wrote:
> question 1) should there be foreign key relations in mysql db?  I used
> the script/generate model and then rake db:migrate

Rails has no native support for foreign key constraints in the DB. I
use the RedHillOnRails Core plugin, unfortunately there main site went
offline not long ago but you can get the plugin from:
http://github.com/harukizaemon/redhillonrails_core.... This
allows you to specify foreign keys in migrations if you want the
database to maintain referential integrity for you (dunno who
wouldn't!).


> question 2) in my conceptual model my developer belongs to a team.  So
> i have the model above.  Should there be a relationship in team
> describing how the link exists to developer? Currently mine is empty:
> class Team < ActiveRecord::Base
> end

You would say has_many :developers. The Rails API
ActiveRecord::Associations documentation provides an in-depth
discussion of relations.

Best regards,
Andrew
8a934bca2d929e50550e5d97e4e0ddcc?d=identicon&s=25 AGoofin (Guest)
on 2009-04-14 03:14
(Received via mailing list)
Your Team has many members and should be noted:

class Team < ActiveRecord::Base
    has_many :members
end

This is assuming that you have a column in your member table named
team_id. I think this is what you were asking about with the foreign
keys.

As a somewhat noob to Rails myself I found a few good books for
beginners to be the most help. The two I like the best are: "Simply
Rails 2" and "Agile Web Development with Rails".
This topic is locked and can not be replied to.