Categorized under Development
[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 Students that have a Record per year. Each record has several Events. These records also have two specific events: a test and an audition, 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 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.
Code sample
December 9th, 2011 —
Leave a comment!
For those who don’t care about astrology, try to follow along; the principle is pretty practical.
TL;DR – Being bossed around compromises inovation, creativity and it destroys one’s true sense of purpose. Don’t let that destroy you.
Read the rest of this entry »
November 12th, 2011 —
Leave a comment!
The company I work for uses Java for all of its projects. Based on that, I have been considering looking into JRuby and giving it a try. Every piece of advice is greatly appreciated.
Read the rest of this entry »
October 20th, 2011 —
6 comments so far
Over these last two months, I’ve been working with Datatables, an amazing easy-to-setup steroid machine for HTML tables. For the kind of work I’m doing right now, it suits my needs just perfectly.
A couple of weeks ago I needed to build a plugin for DT: MultipleFilters, something that would allow me to filter data by a range. Each column would have a pair of fields and I would have to mix them into the bowl of parameters to send to the server . It looked fun, so I started doing it, hoping to achieve a beautiful Javascript prototype object that would do magic when plugged in by a single letter. As I was finishing building it, I realized some issues had to be fixed.
Read the rest of this entry »
September 1st, 2011 —
Leave a comment!
A Ruby developer I met recently told me that the majority of us Rubyists skip documentation in favor of Test Driven Development tools. Some business are still pretty waterfall oriented and demand all the requirements gathering, business analysis and design beforehand, as well as hard documentation with lots of funny terms and fancy diagrams.
I told her I was looking forward to studying Cucumber and Rspec in order to improve my skills. She asked me how I did documentation using those tools and I answered: “Cucumber and Rspec are the documentation”. As she twisted her nose I could tell I had to research more on the topic; so I did.
Read the rest of this entry »
August 2nd, 2011 —
Leave a comment!