Friday night and the progress is slow (but steady)

July 29, 2007

OK – carrying on from yesterday, looking at the new and create methods, for articles.  Trying to work out why new_article isn’t working.  My friend aaron got me in touch with Matt Schnitz, a guy he knows off filepile who knows a lot about rails.  He kindly agreed to help me out, and with his help have almost cracked this link adding problem.

What’s happening now:

I added another line to the definition of the habtm relationship in the model:
:foreign_key => “article_id”,

Then, i changed the ‘add a link’ hyperlink to pass through a parameter called @original_article_id:
‘new_link’, :original_article_id => @article.id %>

Then, i make the association in the ‘create_link’ method by using that parameter to retrieve the original article, and then use the << operator to add the new article to the old one’s links.
@original_article = Article.find(:first, :original_article_id)
@original_article.links << @article 

However, this is still broken – the find statement is always returning the article with id 1, so that the new article is added to the links of article 1, rather than the article we actually called ‘add link’ from.  I believe this is because :original_article_id isn’t being passed through properly.

…several days pass…

OK, with the help of a kindly friend of a friend (Matt Schnitz from San Francisco) and the forums, i got this working.  The first problem i think is that whenever a method call occurs, the params hash is re-created from scratch.  So if you pass something to an action and then it calls another action, you need to get the original parameter and pass it through again:  i did that like this (for the second method call, at the top of the form):

‘create_link’, :original_article_id => params[:original_article_id] do %>

The next problem was how to make a new association in the join table.  This is pretty easy when you know how:  using the passed through parameter of the original article (which the link is added to), i get the article:

@original_article = Article.find(:first, :conditions => [“id = ?”, params[:original_article_id] ]) 

Then, i get the new article from the :id value passed through in params, and add it to the original’s list of links:

@article = @original_article.links.create(params[:article]) 

I could have also done it using the << operator:

#alternative to the above 
@original_article = Article.find(:first, :conditions => [“id = ?”, params[:original_article_id] ])
@article = Article.new(params[:article])
@original_article.links << @article

So, at the end of the week (sunday evening) i’ve managed to get the new feature in at last.  Another amazing last minute rescue!  I’m feeling good about that – this has taken far too long but it’s done.  Next step is the ‘create a story’ functionality.  That’s going to take a bit of design work – tomorrow morning’s task.  For now, though, dinner’s almost ready and it’s chillin time.  woot.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: