Tag Archive for 'has_many'

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 issue cont.

After getting some sleep and then re-reading my earlier post, I realized that I was as not as clear as I’d like to be about my problem with has_many :through.

Just about every example I found of how to use has_many :through showed how to use it to read data and most were explaining how to use it for polymorphic associations. What I needed was to know how to create the data for a non-polymorphic relationship. This highlights a problem with the Rails framework, and the one thing I miss from PHP. Documentation. When looking at the API, I frequently find areas that have no commentary at all, and other areas where the commentary is incomplete. A lot of people have hit brick walls because of this, more than one google for help on a specific problem brought me across a forum post or a blog post describing frustration at the lack of information in the API. Continue reading ‘has_many :through issue cont.’

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