diff options
author | José Mota <josemota.net@gmail.com> | 2012-10-22 12:42:39 +0100 |
---|---|---|
committer | José Mota <josemota.net@gmail.com> | 2012-10-22 12:42:39 +0100 |
commit | 491f496a7143e70ba58cbf25d700267ac96783fa (patch) | |
tree | dac3161fb6f5b7601a5a928c8dad59a5ec501caa /_posts | |
parent | 606a507fff5fc179764b8a0f5d26bc33334ebd86 (diff) |
Convert Rails 3 associations post to Markdown.
Diffstat (limited to '_posts')
-rw-r--r-- | _posts/2011-12-09-same-class-associations-in-rails-3.html | 31 | ||||
-rw-r--r-- | _posts/2011-12-09-same-class-associations-in-rails-3.markdown | 43 |
2 files changed, 43 insertions, 31 deletions
diff --git a/_posts/2011-12-09-same-class-associations-in-rails-3.html b/_posts/2011-12-09-same-class-associations-in-rails-3.html deleted file mode 100644 index 654f69d..0000000 --- a/_posts/2011-12-09-same-class-associations-in-rails-3.html +++ /dev/null @@ -1,31 +0,0 @@ ---- -layout: post -title: Same class associations in Rails 3 -tags: -- Development -status: publish -type: post -published: true -meta: - _edit_last: '1' - _cws_is_markdown: '1' ---- -<strong>[TL;DR]</strong> Even though the selected events conceptually belong to a record, the latter has the foreign keys to former. So technically, <code>has_one</code> is to be changed to <code>belongs_to</code>. - -<hr /> - -This is the first time I've ran into something like this and it was interesting to realize what it actually means when developing a business logic in Rails. - -<h3>Context</h3> - -In this app I'm building, I have <em>Students</em> that have a <em>Record</em> per year. Each record has several <em>Events</em>. These records also have two specific events: a <em>test</em> and an <em>audition</em>, registered in the schema as <code>id</code>'s in the record's table. - -<h3>So what did technically happen?</h3> - -I wasn't able to access those specific events through the associations specified in the model. Given <code>r = Record.first</code>, when I tried to access the audition, by using <code>r.test</code>, Rails would use a SQL query that would correspond to <code>r.events.first</code> instead. - -After acknowledging that, I turned to <a href="http://twitter.com/varandas">@varandas</a> and we both thought it might be a bug in the Rails framework. Turns out it wasn't; all I had to do was switch from <code>has_one</code> to <code>belongs_to</code> (thanks <a href="http://github.com/drogus">@drogus</a>!). The reason for that is the foreign key is on the <code>records</code> table. From the framework's perspective, it looks like the record actually <em>belongs to</em> the event, when in practice it's not. - -<h3>Code sample</h3> - -[gist id=1449428] diff --git a/_posts/2011-12-09-same-class-associations-in-rails-3.markdown b/_posts/2011-12-09-same-class-associations-in-rails-3.markdown new file mode 100644 index 0000000..e7062e4 --- /dev/null +++ b/_posts/2011-12-09-same-class-associations-in-rails-3.markdown @@ -0,0 +1,43 @@ +--- +layout: post +title: Same class associations in Rails 3 +tags: development +type: post +published: true +--- + +**[TL;DR]** Even though the selected events conceptually belong to a record, +the latter has the foreign keys to former. So technically, `has_one` +is to be changed to `belongs_to`. + +* * * + +This is the first time I've ran into something like this and it was interesting +to realize what it actually means when developing a business logic in Rails. + +### Context + +In this app I'm building, I have <em>Students</em> that have a <em>Record</em> +per year. Each record has several <em>Events</em>. These records also have two +specific events: a <em>test</em> and an <em>audition</em>, registered in the +schema as `id`'s in the record's table. + +### So what did technically happen? + +I wasn't able to access those specific events through the associations +specified in the model. Given <code>r = Record.first</code>, when I tried to +access the audition, by using <code>r.test</code>, Rails would use a SQL query +that would correspond to <code>r.events.first</code> instead. + +After acknowledging that, I turned to <a +href="http://twitter.com/varandas">@varandas</a> and we both thought it might +be a bug in the Rails framework. Turns out it wasn't; all I had to do was +switch from <code>has\_one</code> to <code>belongs\_to</code> (thanks <a + href="http://github.com/drogus">@drogus</a>!). The reason for that is the + foreign key is on the <code>records</code> table. From the framework's + perspective, it looks like the record actually <em>belongs to</em> the event, + when in practice it's not. + +### Code sample + +<script src="http://gist.github.com/1449428.js"></script> |