<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
   <channel>
      <title>MyInnoQBlog</title>
      <link>http://www.innoq.com/blog/dp/</link>
      <description>by Daniel Pietzsch</description>
      <language>de</language>
      <copyright>Copyright 2008</copyright>
      <lastBuildDate>Tue, 29 Apr 2008 19:30:30 +0100</lastBuildDate>
      <generator>http://www.sixapart.com/movabletype/</generator>
      <docs>http://blogs.law.harvard.edu/tech/rss</docs> 

            <item>
         <title>Lesson learned</title>
         <description><![CDATA[A part of my ER-Model is not correct:

<a href="http://www.innoq.com/blog/dp/2008-04-29_er_diagramm_ausschnitt_wrong.png"><img src="http://www.innoq.com/blog/dp/2008-04-29_er_diagramm_ausschnitt_wrong.png" width="500" height="206" title="Click to view bigger"></a>

The many-to-many association between _Projects_ and _Users_ is simply wrong. Imagine a simple case of an deletion of a relation between an _User_ and a _Project_ - for example the user/employee stopped working on that particular project - causes the project also to disappear from _Timereports_ in the past.  
And this is fundamentally wrong, **because he surely did work on that project**!

And so the connection should be between _Projects_ and _Timereports_!!! Like this:

<a href="http://www.innoq.com/blog/dp/2008-04-29_er_diagramm_ausschnitt_right.png"><img src="http://www.innoq.com/blog/dp/2008-04-29_er_diagramm_ausschnitt_right.png" width="500" height="162" title="Click to view bigger"></a>

I'm pretty sure this has to be modelled this way.  
Now if the user stops working on a particular project, it's no problem to do that deletion.

	Project has_many :timereports, :through => :projects_timereports
	######
	Timereport has_many :projects, :through => :projects_timereports
	######
	ProjectsTimereports has_many :timereportentries
	
Ok, now I'll have to re-code that and I hope it won't me drive nuts to do this, because I really could have avoided it!]]></description>
         <link>http://www.innoq.com/blog/dp/2008/04/lesson_learned.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/04/lesson_learned.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
        
        
         <pubDate>Tue, 29 Apr 2008 19:30:30 +0100</pubDate>
      </item>
            <item>
         <title>ActiveResource and Associations</title>
         <description><![CDATA[Ok, it's been quite a while since the last post popped up here; that's because I struggled with the innoQ internal timereporting- and invoicing-application I'm working on, which is my diploma thesis, too.  
And here's one of my problems I had...

### Preface

Concerning users that are going to use my application, there has been a change made, so my app is no longer responsible for user management. This is now all done by a separate application. And this applications has a REST interface which I have access to and should obtain my user data from, as well as handle authentication.
I now look at the "obtain data" part, because the authentication part is definitely worth another blog post.

### ActiveResource basics

Now: receiving data from another Rails app with a REST interface is quite simple. In the model (in my case _User_) you inherit from _ActiveResource::Base_ instead of _ActiveRecord::Base_ and specify a site url to the remote Rails application. Like this:

    class User < ActiveResource::Base
		self.site = "http://the.url.to/yourapp/"
	end
	
Now you can obtain user data almost the same way you do with _ActiveRecord_ classes:

	User.find(2) # finds the user with the id of '2'
	
or even

	User.find(:all) # finds all users in the remote app
	
if you like.

### My problem

But what if your resource is somehow associated with your local model and database?  
In my case an _user_ has many _projects_ and a _project_ has many _users_. So here you have a many-to-many relationship.

That really is more difficult for problems like "I'd like to obtain all users for a specific project" and vice versa, because you can't just make up a many-to-many relationship as you would with two ActiveRecord objects. Like this:

	has_and_belongs_to_many :projects # or
	has_many :projects, :through => 'projects_users'
	
In a class which inherits from ActiveResource this is not supported/allowed.  
Ok, in my _project_ model these statements would work, because it is an ActiveRecord object. But all the methods you gain through this won't work either, because they don't know the you are referencing a model of type ActiveResource and all finds will fail.

So what to do?  
Here's what works for me (so far).  
But I'm sure there are many more ways out there to solve this, and I'd really appriciate every comment on this topic, 'cause I am curious if there's a more elegant solution. So, there you go:

### My solution

Remember the initial situation:

Project (ActiveRecord - in my app) (M)-----------(has)----------(N) User (ActiveResource - remote app)

So first of all I created a join table and the corresponding join model by typing (in the console, of course):

	script/generate model projects_users
	
This creates a model for me with along with a migration file. Inside this file I write these lines:

	class CreateProjectsUsers < ActiveRecord::Migration
	  def self.up
	    create_table :projects_users do |t|
	      t.integer :project_id, :user_id, :null => false
	      t.string :color, :null => false
	    end
	  end

	  def self.down
	    drop_table :projects_users
	  end
	end
	
An advantage of join models is that you can store additional attributes in the join table. The only thing I store along the _project\_id_ and the _user\_id_ is a _color_. That's just relevant for the front-end to display the project in the correct color, so don't get irritated. ;-)

Then, I ran the migration:

	rake db:migrate
	
Now, let's head over to the association part.  
As you can't put any associations in a ActiveResource model, there's nothing to do in the _User_ model.

In the _Project_ model I put a _has\_many_ statement:

	has_many :projects_users, :class_name => "ProjectsUsers"
	
(Note, that in my case I had to explicitly tell the _class\_name_ of the join model class, because Rails assumes this to be singular, i.e. _ProjectUser_.)

In the _ProjectsUsers_ model I wrote the following two lines:

	belongs_to :project
	belongs_to :user # so far this isn't used by me, but it may be useful some day
	
Now the interesting part: How can you get all users for a given project in a way like _project.users.find(:all)_?  
In _project.rb_, define a method called _users_:

	def users
    	user_ids = self.projects_users.find(:all, :select => "user_id")
    	users = user_ids.collect { |projects_users| User.find(projects_users.user_id) }
	end
	
Now via _project.users_ you'll get all the users you need.  
(I'm pretty sure you can rewrite this, so that it works the _railsway_ and you can call _project.users.find(:all)_ instead, but I haven't tried it, yet)

Ok, the other way round, meaning obtaining all projects for a specific user, I faked it a bit, and I'm sure you can do better here, too. But it works!  
Instead of creating a _projects_ method in the _User_ model, I coded a method called _self.find\_by\_user\_id_ in the _Projects_ model:

	def self.find_by_user_id(userid)
    	project_ids = ProjectsUsers.find_all_by_user_id(userid, :select => "project_id")
    	projects = project_ids.collect { |projects_users| projects_users.project }
	end
	
You can call it this way:

	user_projects = Project.find_by_user_id(2)
	
And _BOOM!_, you have all projects for the user no. 2.

That's all folks!
This was my solution/workaround for integrating a REST-Resource via a many-to-many association in Rails. Hope you liked it. :-)]]></description>
         <link>http://www.innoq.com/blog/dp/2008/04/activeresource_and_association.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/04/activeresource_and_association.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Ruby on Rails</category>
        
                  <category domain="http://www.sixapart.com/ns/types#tag">ActiveRecord</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">ActiveResource</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">Associations</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">habtm</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">has_many</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">join</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">many-to-many</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">model</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">rails</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">Ruby</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">RubyOnRails</category>
        
         <pubDate>Wed, 23 Apr 2008 19:39:50 +0100</pubDate>
      </item>
            <item>
         <title>Rails and in_place_editor issues</title>
         <description><![CDATA[Ok, so the last few days I had a bit of a struggle concerning the [script.aculo.us in place editor](http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor) and the corresponding [plugin](http://dev.rubyonrails.org/browser/plugins/in_place_editing "/plugins/in_place_editing - Rails Trac - Trac")/helper in rails.  
So here are my links that really helped me solving these issues:

* [InPlaceEditing in Ruby on Rails](http://wiki.rubyonrails.org/rails/pages/InPlaceEditing "InPlaceEditing in Ruby on Rails") (in case you get an error like *"Called id for nil, which would mistakenly be 4—if you really wanted the id of nil, use object_id"*)
* [in\_place\_edit\_for, in\_place\_editor\_field and Complex Cases](http://www.postal-code.com/binarycode/2007/04/13/in_place_edit_for-in_place_editor_field-and-complex-cases/ "Binary Code &raquo; in_place_edit_for, in_place_editor_field and Complex Cases") (if it's not the 'standard' way you want to use the editor for) and
* [can't get in\_place\_edit to work in rails 2.0](http://www.ruby-forum.com/topic/136093 "can't get in_place_edit to work in rails 2.0 =&gt; ActionController::InvalidAuthenticityToken - Ruby Forum") (if you use cookie based session data and turned on the *protect\_from\_forgery* secret key; actually the 'solution' given in this thread is more a workaround - but useful anyway)

If you have more interesting links regarding the in\_place\_editor and rails, feel free to leave a comment right here. Thanks.]]></description>
         <link>http://www.innoq.com/blog/dp/2008/04/rails_and_in_place_editor_issu.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/04/rails_and_in_place_editor_issu.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Ruby on Rails</category>
        
                  <category domain="http://www.sixapart.com/ns/types#tag">errors</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">in_place_editor</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">rails</category>
        
         <pubDate>Sat, 05 Apr 2008 15:58:40 +0100</pubDate>
      </item>
            <item>
         <title>Euruko this weekend! [UPDATE]</title>
         <description><![CDATA[This weekend the <a href="http://www.euruko2008.org/">Euruko conference</a> takes place in Prague. It's all about Ruby and I'll gonna be there.  
I'm quite excited, as this is my first (big) conference I'm going to attend. I think most of <a href="http://www.euruko2008.org/pages/2-program">the talks</a> will be pretty advanced for a Ruby beginner like me, but I think I won't get dumber... ;-)

Unfortunately we'll probably miss Matz' Keynote, because our flight arrives at 09:30 in the morning and to think we'll be in the center of Prague at 10 o'clock would be really optimistic...

**[UPDATE]**
We managed to get to prague university almost in time, so we saw most of the keynote:

<a href="http://www.flickr.com/photos/pie4dan/2372845245/" title="Venue by pie4dan, on Flickr"><img src="http://farm4.static.flickr.com/3269/2372845245_dee9184d0e.jpg" width="500" height="333" alt="Venue" /></a>

I'm also looking forward to visiting Prague again, 'cause it really is a beautiful city - and affordable, too. That's why I'll stay until late tuesday. :-)]]></description>
         <link>http://www.innoq.com/blog/dp/2008/03/euruko_this_weekend_update.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/03/euruko_this_weekend_update.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">About</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Ruby</category>
        
                  <category domain="http://www.sixapart.com/ns/types#tag">euruko2008</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">Ruby</category>
        
         <pubDate>Thu, 27 Mar 2008 20:32:39 +0100</pubDate>
      </item>
            <item>
         <title>More ER-Modelling</title>
         <description><![CDATA[Once more, I improved the model of my application. I wasn't satisfied with the fact, that I was indeed able to store the time report data for each day of the month, but have an [UI](http://www.innoq.com/blog/dp/2008/02/third_and_probably_last_time_r.html "Third (and probably last) time reporting mockup (MyInnoQBlog)") on a monthly basis and so **one** screen for multiple *DailyReports*.  
Now I modelled it the way, that I have a (monthly) *TimeReport* which has many *DailyReports*. This way a *DailyReport* belongs to a *Project* and a (monthly) *TimeReport*. And a *TimeReport* belongs to a specific user/employee.

<a href="http://www.innoq.com/blog/dp/2008-03-11_er_model_v07_excerpt.png"><img src="http://www.innoq.com/blog/dp/2008-03-11_er_model_v07_excerpt.png" width="500" height="230"></a>

That's exactly how I wanted it to be from the beginning. I don't no why this took me this long...

Now it's also no problem to get nice URLs for a *TimeReport*. For example like this: *http://.../TimeReport/DanielPietzsch/2008/3/* for March 2008.]]></description>
         <link>http://www.innoq.com/blog/dp/2008/03/more_ermodelling_1.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/03/more_ermodelling_1.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
        
        
         <pubDate>Tue, 11 Mar 2008 13:19:54 +0100</pubDate>
      </item>
            <item>
         <title>Third (and probably last) time reporting mockup</title>
         <description><![CDATA[Besides [thinking about how to model my database](http://www.innoq.com/blog/dp/2008/02/altered_ermodel.html "Altered ER-Model (MyInnoQBlog)") I continued working on the UI:

<a href="http://www.innoq.com/blog/dp/2008-02-28_time_reporting_mockup_3.png"><img src="http://www.innoq.com/blog/dp/2008-02-28_time_reporting_mockup_3.png" width="500" height="370" /></a>

Ok, maybe you don't see that much difference since the last mockup, but I refined most of the div-structure beneath the surface and did some minor design changes.  
But the biggest improvement is, that the calendar is now based on real dates. I must admit that the [calendar helper plugin](http://agilewebdevelopment.com/plugins/calendar_helper "Plugins - Calendar Helper - Agile Web Development") helped me with the code.

The bullet-points on the 28th are a representation for all calendar fields, where you entered any hours. If you didn't enter an other number than *0* the day cell stays empty (for display). If you want the add or modify hours on a particular day, you click the cell and via an in-place editor you can enter your information. (That's the plan).

I'm not quite sure if the coloring of the weekends will stay that way...I think I'll decide later, since this is not the most important decision to make.

Now I think I should concentrate on the real app, meaning all the functionality and model behind the UI.]]></description>
         <link>http://www.innoq.com/blog/dp/2008/02/third_and_probably_last_time_r.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/02/third_and_probably_last_time_r.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
        
                  <category domain="http://www.sixapart.com/ns/types#tag">calendar</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">mockup</category>
        
         <pubDate>Thu, 28 Feb 2008 18:27:54 +0100</pubDate>
      </item>
            <item>
         <title>Altered ER-Model [UPDATE]</title>
         <description><![CDATA[I altered a part of [my ER-Model](http://www.innoq.com/blog/dp/2007/08/erdiagramm_bestellungenauftrag.html "ER-Diagramm: Bestellungen/Aufträge (MyInnoQBlog)") concerning the implementation of the time-reporting:

<a href="http://www.innoq.com/blog/dp/2008-02-26_er_modell_excerpt.png"><img src="http://www.innoq.com/blog/dp/2008-02-26_er_modell_excerpt.png" width="500" height="221" /></a>

There's no longer an entity called "Arbeitszeit" (working time). All the worked hours and days should now get stored in the extra many-to-many-table that'll be created in between "Stundenzettel" (timesheet) and "Projekt" (project). That table might have the following attributes: *StundenzettelID, ProjektID, workingTimeTotal, dailyTimes, notes*.  
So far I'm not sure how to store all the hours to their corresponding days in *dailyTimes* (because that's at least about 30 values every month and user), but I might use a plain text field which contains a somehow-delimited list of working-hours (ascending from the 1st to the last day of the month).

The "Mitarbeiter" (co-worker) entity is also the applications entity for storing login- and user-information.

What you see on the [latest mockup](http://www.innoq.com/blog/dp/2008/02/second_ui_mockup.html "Second UI mockup (MyInnoQBlog)"), is in Rails the update- or new-action/view for the "Stundenzettel"-controller. I think there won't be any implementation of a read- or delete-action for "Stundenzettel" (timesheets).

So, I hope this will work for me...

And yes, next time the ER-Model will be in english, too, that I don't have to bother you (and me) with this language mix. I also code in english, so this makes sense anyway.

[UPDATE]

After talking to [Tim](http://innoq.com/blog/tk/) and [Phillip ](http://ghadir.de/blog/) *and* considering [Stefans Comment](http://www.innoq.com/blog/dp/2008/02/altered_ermodel.html#comment-251726), I think this is the right way of modelling the Entity Relationship:

<a href="http://www.innoq.com/blog/dp/ER-Diagramm%20V0.6%20Ausschnitt.png"><img src="http://www.innoq.com/blog/dp/ER-Diagramm%20V0.6%20Ausschnitt.png" width="500" height="224" /></a>

So, in the *DailyReport* entity there is the attribute *Date* which is an additional primary key to the *ProjectID* and the *UserID*. This way the *WorkedHours* are stored for every day. So you have a table for every day, employee and project.  
The same with *notes*. This may be useful to describe your tasks on that day, for example. This way I think I'll have to reengineer the UI, 'cause notes do not get stored on a monthly basis. I already have something in mind for that.

[/UPDATE]]]></description>
         <link>http://www.innoq.com/blog/dp/2008/02/altered_ermodel_update.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/02/altered_ermodel_update.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Ruby on Rails</category>
        
        
         <pubDate>Tue, 26 Feb 2008 15:08:03 +0100</pubDate>
      </item>
            <item>
         <title>Second UI mockup</title>
         <description><![CDATA[The second mockup of the applications UI. I thought I stick to the design of [innoq.com](http://innoq.com/), so co-workers are somehow familiar with the layout. The (div-)structure underneath is quite different than the one of innoq.com, but I copied some design-elements so far.

Take a look:

<a href="http://www.innoq.com/blog/dp/2008-02-22_time_reporting_2.png"><img src="http://www.innoq.com/blog/dp/2008-02-22_time_reporting_2.png" width="500" height="370" /></a>

In the top-right corner you now have the ability to add one or more projects you're currently working on. They'll be differentiated by colors. In the calendar you'll find the corresponding fields for these projects. Again: I think I'll hide most of these field in normal view, using an in-place editor.

I'm still really curious what your opinion is...]]></description>
         <link>http://www.innoq.com/blog/dp/2008/02/second_ui_mockup.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/02/second_ui_mockup.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
        
        
         <pubDate>Fri, 22 Feb 2008 18:52:41 +0100</pubDate>
      </item>
            <item>
         <title>First UI mockup</title>
         <description><![CDATA[As far as [Unified Process](http://de.wikipedia.org/wiki/Rational_Unified_Process "Rational Unified Process - Wikipedia") is concerned, I already clarified some of the Business Modeling and Requirements a few month ago. See my blogposts [here](http://www.innoq.com/blog/dp/2007/08/erdiagramm_bestellungenauftrag.html "ER-Diagramm: Bestellungen/Aufträge (MyInnoQBlog)") and [here](http://www.innoq.com/blog/dp/2007/07/usecase_diagramm_nachste_versi.html "Use-Case Diagramm: nächste Version (MyInnoQBlog)").

Well, I think I had a flash of inspiration: in order to "[get real](http://gettingreal.37signals.com/ "Getting Real: The Book by 37signals")" I decided to start with the project. Isn't that great?  
And I decided to go with the UI first, because the first thing that comes to my mind is how the app will roughly look like. As a working title I called the app "innoVoice" (Yeah, it really *is* a great pun *\*cough\**)

There you go:

<a href="http://www.innoq.com/blog/dp/2008-02-18_time_reporting.png"><img src="http://www.innoq.com/blog/dp/2008-02-18_time_reporting.png" width="500" height="380" /></a>

That is the first screen for the colleagues out there in customer-projects for doing their time-reporting.  
Ok, it is still made up of standard fonts, there are no fancy graphics etc., but I think it represents the image I have in mind when I think of that part of the app.  
What bothers me a bit are all the input fields in the calendar. I think I'll hide them using some AJAX in-place editors, so they won't be that distracting.

I don't really know what will be next. Either I start implementing this feature or I go for another UI-mockup. Maybe I'll know more in one hour or so...

Stay tuned and feel free\* to comment on the UI-mockup!

<small>\*actually, I would force you to comment, if I could!</small>]]></description>
         <link>http://www.innoq.com/blog/dp/2008/02/first_ui_mockup_1.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/02/first_ui_mockup_1.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
        
        
         <pubDate>Mon, 18 Feb 2008 16:47:02 +0100</pubDate>
      </item>
            <item>
         <title>Preface</title>
         <description><![CDATA[The first draft of what my diploma thesis will be about (in german): <a href="http://www.innoq.com/blog/dp/Arbeitsthese.pdf">Arbeitsthese</a> (PDF)]]></description>
         <link>http://www.innoq.com/blog/dp/2008/02/preface.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/02/preface.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">About</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
        
        
         <pubDate>Thu, 14 Feb 2008 14:28:09 +0100</pubDate>
      </item>
            <item>
         <title>Years of irrelevance</title>
         <description><![CDATA[<blockquote>"Requiring X years of experience on platform Y in your job posting is, well, ignorant."</blockquote>
DHH on the <a href="http://www.37signals.com/svn/posts/833-years-of-irrelevance">37signals blog</a>.

I found this <a href="http://www.37signals.com/svn/posts/833-years-of-irrelevance">post</a> really interesting (including the comments).

As a student I never applied for a job that required X years in experience on a specific platform, but I sometimes wondered how hard employers insist on such a point on a job offer.  
I mean, it really depends on the single person. There a definitely programmers (not to say geeks ;-) ) that are better in programming X after one year of experience than others are after - say - 3 years (IMHO).

What do you think?]]></description>
         <link>http://www.innoq.com/blog/dp/2008/02/years_of_irrelevance.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/02/years_of_irrelevance.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Web</category>
        
                  <category domain="http://www.sixapart.com/ns/types#tag">37signals</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">DHH</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">programming</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">YOE</category>
        
         <pubDate>Tue, 05 Feb 2008 19:48:51 +0100</pubDate>
      </item>
            <item>
         <title>Hell yeah! I&apos;m done...</title>
         <description><![CDATA[...with written tests for university!  
Today i wrote my last and now I can fully concentrate on my diploma thesis (and innoq.com, too). By the way: <a href="http://www.innoq.com">innoq.com</a> is running on Rails for almost 3 weeks now, and I think it's doing fine, isn't it, <a href="http://innoq.com/blog/st/">Stefan</a>?

I think I'll have a beer right now...! Cheers!]]></description>
         <link>http://www.innoq.com/blog/dp/2008/01/hell_yeah_im_done.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/01/hell_yeah_im_done.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">About</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Diploma</category>
                  <category domain="http://www.sixapart.com/ns/types#category">innoq.com</category>
        
                  <category domain="http://www.sixapart.com/ns/types#tag">diploma</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">innoq.com</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">rails</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">tests</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">university</category>
        
         <pubDate>Wed, 30 Jan 2008 18:35:30 +0100</pubDate>
      </item>
            <item>
         <title>Got Math?</title>
         <description><![CDATA[Amy Hoy:

<blockquote>"No, [math is] not a prerequisite for most programming, and especially not web development as a whole.

The more I watch people struggle with programming, the more I think that synthesis and intellectual flexibility are far more important skills than the ability to write small, isolated, brilliant bits of code."</blockquote>

Really good article and discussion on the link between math and programming skills.  
Be sure to read the very interesting comments as well:

<a href="http://www.slash7.com/articles/2008/1/2/got-math">http://www.slash7.com/articles/2008/1/2/got-math</a>]]></description>
         <link>http://www.innoq.com/blog/dp/2008/01/got_math.html</link>
         <guid>http://www.innoq.com/blog/dp/2008/01/got_math.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
        
                  <category domain="http://www.sixapart.com/ns/types#tag">math</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">programming</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">skills</category>
        
         <pubDate>Thu, 03 Jan 2008 10:34:02 +0100</pubDate>
      </item>
            <item>
         <title>AJAX calls with fallback</title>
         <description><![CDATA[When you have a list of - say - articles (that's what I have here) and you want to change the order the items appear on the fly(maybe the newest ones first, or alphabetic or maybe clockwise...you name it...), then you have two possibilities:  
The _old_ way by providing a link that sends a "normal" request to the server which returns the same site again, with the difference of an alternated order of articles.  
In rails this is pretty basic stuff:

    <%= link_to 'Oldest ones first', :action => "index",
    :sort_string => "created_at ASC" %>

The index-action in the controller then makes nothing different than before besides doing a database-query with the altered order-statement. And returns the site. Here the excerpt from the controller:

	def index
	if params[:sort_string]
  		sort_string = params[:sort_string]
	else
  		sort_string = "created_at DESC" # the default sort order
	end

	@articles = Article.find(:all, :order => sort_string)
	...

And there is the new _cool_, _web 2.0_ way: performing an AJAX call to the controller and just update the section of the page that needs to be updated. Without having to refresh the whole site. There you go:

	<%= link_to_remote 'Oldest ones first', :url => { :action => "index",
	:sort_string => "created_at ASC" }, :method => :get %>
	
This doesn't work without two more lines of code. But that's done with the blink of an eye.  
In the index action of the controller you need to respond to the javascript format by adding this:

	... # excerpt
	respond_to do |format|
  		format.html # index.html.erb
  		format.js # index.js.rjs THIS LINE IS ADDED
  		format.xml  { render :xml => @articles }
	end
	... # excerpt
	
Further you need to create a file with name _index.js.rjs_\* in /app/views/YOURCONTROLLER/.
Add the following line of code to this file:

    page.replace_html :list, :partial => "list"

...where _:list_ is the id of your div-element that contains the list of articles.  
_:partial => "list"_ calls the \_list.html.erb partial (which contains that specific div).

**The list must be wrapped in a div-element and the whole thing goes into \_list.html.erb!!!**  
So just copy the code of the list you already have to the partial file and wrap everything in a div with id "list". Then call the partial from that point where you removed it with _<%= render :partial => "list" %>_.

And then you're done. When you hit the link, the list refreshes without loading the whole site again.

But wouldn't it be nice to make this feature always work, even for browsers that have JavaScript disabled? I think so, 'cause it guarantees that this works without relying on an enabled JavaScript setting.
And that's super-easy. Just modify the link in the view this way:

	<%= link_to_remote 'Oldest ones first', {:url => { :action => "index",
	:sort_string => "created_at ASC" }, :method => :get }, :href =>
	url_for(:action => "index", :sort_string => "created_at ASC") %>
	
The only thing I had to add was two braces and the stuff following _:href =>_.  
If JavaScript is enabled, the AJAX call will be send and otherwise it works as a "normal" call.
Nice, heh?!?

But as you can see, the parameters passed are duplicates. The _:url_ and _:href_ hashes are quite similar. To DRY\*\* things up, just write a small helper-method in _application\_helper.rb_, that does this duplication for you:

	def link_to_remote_with_fallback(name, options = {}, html_options = {})
    	html_options[:href] = url_for(options[:url])
    	link_to_remote(name, options, html_options)
  	end

And BOOM! The code's DRY again:

    <%= link_to_remote_with_fallback 'Oldest ones first', :url =>
    { :action => "index", :sort_string => "created_at ASC" } ,
    :method => :get %>
	
Credits: [DRYing up link_to_remote for degradable URLs](http://blog.codefront.net/2007/03/24/drying-up-link_to_remote-for-degradable-urls/ "DRYing up link_to_remote for degradable URLs - redemption in a blog")

<small>\*_index_ is the name of the action in the controller. So modify this to match your action.<br/>
\*\*DRY - [Don't repeat yourself](http://en.wikipedia.org/wiki/Don't_repeat_yourself "Don't repeat yourself - Wikipedia, the free encyclopedia")</small>]]></description>
         <link>http://www.innoq.com/blog/dp/2007/12/ajax_calls_with_fallback.html</link>
         <guid>http://www.innoq.com/blog/dp/2007/12/ajax_calls_with_fallback.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Ruby on Rails</category>
                  <category domain="http://www.sixapart.com/ns/types#category">innoq.com</category>
        
                  <category domain="http://www.sixapart.com/ns/types#tag">AJAX</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">fallback</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">list</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">order</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">RJS</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">Ruby On Rails</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">sort</category>
        
         <pubDate>Thu, 20 Dec 2007 13:15:09 +0100</pubDate>
      </item>
            <item>
         <title>Doing AJAX-links right in Rails 2.0</title>
         <description><![CDATA[Ok, maybe this is a trivial topic, but as this was my first "real" AJAX experience in Rails 2.0, I spent some time solving this issue:

I wanted to sort a list of articles differently using AJAX. So in the appropriate action of the controller I added the _format.js_ line:

    respond_to do |format|
		format.html # index.html.erb
  		format.js
  		format.xml  { render :xml => @articles }
	end

And then I created a new view template in the view folder belonging to that controller: _index.js.erb_.  
This was my first fault! The correct file extension is _.js.rjs_! First this didn't make sense to me, but I got some explanation here: [http://www.railsforum.com/viewtopic.php?pid=47149](http://www.railsforum.com/viewtopic.php?pid=47149)

And then - in my _index.js.rjs_ template - I wrote the following line:

    page.replace_html :list, :partial => "list"

This replaces the HTML of the div _list_ with a rendering of the partial \_list.html.erb. I never had any doubt that this line was correct. ;-)  
So far so good, but I got really ugly results clicking a _link\_to\_remote_ link:

	<%= link_to_remote 'Oldest Articles first', :update => 'list',
	:url => { :action => "index", :sort_string => "created_at ASC" }, :method => :get %>
	
The result was something like:

	try { Element.update ...
		
...followed by more strange ruby code, mixed with some of my list data in between, replacing the _list_-div. Here I expected the newly rendered, resorted list.

The problem was the _:update => 'list'_ statement. That way, the code was not interpreted as JavaScript and got directly inserted into the div-tag. And as you can see, I specified to div to be updated in the _page.replace\_html_ call in _index.js.rjs_.

So, the correct call of _link\_to\_remote_ would be without the _:update_ parameter:

    <%= link_to_remote 'Oldest Articles first', :url => { :action => "index",
    :sort_string => "created_at ASC" }, :method => :get %>

Problem solved!

**In short:**

1. the right file extension for responding to JavaScript is _.js.rjs_
2. when using _link\_to\_remote_ with _page.replace\_html_ don't use the _:update_ parameter

As I said above, this is not such a big problem when done correctly, but sometimes it's an accumulation of 2 or 3 little issues that turn into a big one. At least for me.]]></description>
         <link>http://www.innoq.com/blog/dp/2007/12/doing_ajaxlinks_right_in_rails_1.html</link>
         <guid>http://www.innoq.com/blog/dp/2007/12/doing_ajaxlinks_right_in_rails_1.html</guid>
                  <category domain="http://www.sixapart.com/ns/types#category">Dev</category>
                  <category domain="http://www.sixapart.com/ns/types#category">Ruby on Rails</category>
                  <category domain="http://www.sixapart.com/ns/types#category">innoq.com</category>
        
                  <category domain="http://www.sixapart.com/ns/types#tag">2.0</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">AJAX</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">link_to_remote</category>
                  <category domain="http://www.sixapart.com/ns/types#tag">Ruby On Rails</category>
        
         <pubDate>Sat, 15 Dec 2007 17:06:25 +0100</pubDate>
      </item>
      
   </channel>
</rss>
