---
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'
---
[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
.
id
's in the record's table.
r = Record.first
, when I tried to access the audition, by using r.test
, Rails would use a SQL query that would correspond to r.events.first
instead.
After acknowledging that, I turned to @varandas 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 has_one
to belongs_to
(thanks @drogus!). The reason for that is the foreign key is on the records
table. From the framework's perspective, it looks like the record actually belongs to the event, when in practice it's not.