Tag Archive for 'JoinModels'

has_many :oops

A couple of days ago I posted about my has_many :through troubles. Going back and re-reading the section in the new AWDwR 2nd ed. I realized why it is important to be well rested when trying to solve a problem.

When I had read through that section the first time, I did not see what I just read, namely the simple straight forward solution to the problem I was having with creating a join record using a join model. Specifically the bottom half of page 339 demonstrates the solution to the problem I was having.

I mistakenly reported that the book did not discuss the problem at all and I just wanted to clear that up and say it actually did.

Still it is worth noting that a simple google search turns up a lot of instances of people struggling with how to use has_many :through, since it works differently that any of the other association declarations.

Sphere: Related Content

has_many :through antics

Update: I mistakenly reported that AWDwR did not present the solution to my problem. It did on page 339. Please see my later post for specifics.

So in my effort to get back into the swing of things with Ruby and Rails I decided to write an app to manage my dj mixes. The app would allow for entering a mix with a tracklist and download sets. It will also provide an itunes optomized rss feed for podcasting and eventually the ability to digest Traktor playlists and dynamically generate cue sheets.

Initially I worked on getting the mix model and controller working which was pretty easy. After that I created the tracklist controller with the tracklist and track models. Here I struggled. the tracklist model was a join model. I was going to use habtm but I needed to store the track order (and later track start times for cue sheets). So, like a good little ruby nuby I set up a has_many :through relationship between mixes and tracks through tracklists.

Now I thought that I could create a@mix instance and pass it to the tracklist controller, then use @mix.tracks.create(params[:track]) to create the track and and the association record in the tracklist model. Unfortunately it wouldn’t work. Continue reading ‘has_many :through antics’

Sphere: Related Content