diff options
author | José Mota <josemota.net@gmail.com> | 2012-06-03 13:07:33 +0100 |
---|---|---|
committer | José Mota <josemota.net@gmail.com> | 2012-06-03 13:07:33 +0100 |
commit | 276e2edc33ba67ce762dc10a522c07b419cd1429 (patch) | |
tree | e5687404a96b04afee7d9efaf65bb0d171a0f9cb /_posts/2009-04-30-relationships-in-rails-2-3.markdown | |
parent | 88265d0ab7598d6388d6662817915ab9bfda7ed4 (diff) |
Update some more posts.
Diffstat (limited to '_posts/2009-04-30-relationships-in-rails-2-3.markdown')
-rw-r--r-- | _posts/2009-04-30-relationships-in-rails-2-3.markdown | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/_posts/2009-04-30-relationships-in-rails-2-3.markdown b/_posts/2009-04-30-relationships-in-rails-2-3.markdown new file mode 100644 index 0000000..8d1b169 --- /dev/null +++ b/_posts/2009-04-30-relationships-in-rails-2-3.markdown @@ -0,0 +1,77 @@ +--- +layout: post +title: Relationships in Rails 2.3 +tags: [ development, rails ] +type: post +published: true +--- + +Ruby on Rails is becoming quite a piece of software. I've been learning how to +work with its latest version to date, 2.3, for a series of workshops on web +development. It has surprised me how easy it is now to accomplish a simple task +such as multi-model form processing. Let me show what I've been doing lately. + +## Case study: a school + +The example I am going to show my attendees resembles a typical school +situation. In a nutsheel: + +* Students have Subjects. +* Subjects have Students. +* Students have Grades to Subjects. + +![Rails relationships — student](/wp-content/uploads/2009/07/rails-relationships-student.png) + +## The Models + +### Student + +{% highlight ruby %} +class Student < ActiveRecord::Base + has_many :grades + has_many :subjects, :through => :grades, :uniq => true + accepts_nested_attributes_for :grades, :allow_destroy => true +end +{% endhighlight %} + +### Subject + +{% highlight ruby %} +class Subject < ActiveRecord::Base + validates_uniqueness_of :shortname + has_many :grades + has_many :students, :through => :grades, :uniq => true +end +{% endhighlight %} + +### Grade + +{% highlight ruby %} +class Grade < ActiveRecord::Base + belongs_to :subject + belongs_to :student + validates_presence_of :value +end +{% endhighlight %} + +## And the magic trick! + +Through `accepts_nested_attributes_for`, the student's `form_for` can now +accept nested fields through `fields_for` for setting Grades' data. + +Everytime a student is created, subjects are not directly associated to him. +You need to create a form in which you allow to assign a student the subjects +you want. + +[image missing] + +This tells you that a student can have an array of subject id's. You even get +to see what subjects are already assigned and dissociate them just like that! +When the associations are done, a new Grade record is created to associate the +first two modules. Unfortunately, when the associations are dismantled, the +Grade association is gone too. + +If you know Railscasts, the [Complex +Forms](http://railscasts.com/episodes?search=complex+forms) series explain this +task for Rails < 2.3. But now it has become easier and easier to associate +models and worrying less about your structure. |