Yesterday I was having a lot of issues getting ActiveRecord relationships to work.
The problem, as with my relationships with women, was all to do with my lack of understanding. 🙂
I posted up on the rails forum about my dilemma, and was lucky enough to get some fantastic help from one dude in particular , Duplex, who managed to walk me through all my errors and misunderstanding. Check it out here. I look like a retard!
So for my own benefit I am going to do a walkthrough here on ‘News from Above’ (how wanky does that sound?!) of what I did yesterday just to make sure I can.
This example with be an application that has a ‘job’ model (as in a job for a tv commercial) and a ‘director’ model (a tvc director).
———————————————
Create a rails project
in a shell/terminal change into a directory where you will have your RoR’s projects. Then enter:
rails yourprojectname
my app for this will be called ‘kapow’, and I want to use mysql as the database, not the default SQLite DB that comes with osx 10.5.
rails kapow -d mysql
next change directories into the new project
cd kapow
now we should create the database. There are probably many ways to do this but the following seems to be the most simple. using the ruby make command ‘rake’.
rake db:create
If all went well all that should come back in the shell/terminal is something like:
in /pathtoyourproject/kapow)
now we need to build a ‘scaffold’ of the job model
The Job model that is created will have a ‘name’ attribute and a ‘director_id’ attribute.
ruby script/generate scaffold job name:string director_id:integer
I originally thought this is where I would want a ‘string’ field to store the director’s name in, but we are going to create another table for ‘director’. The ‘job’ table will simply store the ‘director_id’ number (actually the ‘id’ of the entry in the ‘director’ table).
Next we need to generate a scaffold for the ‘director’ model
ruby script/generate scaffold director name:string
Cool! Models are nearly ready. Now we need to open up the ruby files for the ‘director’ and ‘job’ models, and explain how they are associated – what their ‘relationship’ is.
A ‘migration’ file has been created in the ‘db’ folder. Now it just had to be run to populate the database with the models.
rake db:migrate
*From this point on it is all pretty much text editing. I use TextMate.
For this little app, the relationships in simple BOLD english are “A DIRECTOR HAS MANY JOBS” and “A JOB BELONGS TO A DIRECTOR”.
So in RoR’s terms for the director model – (“kapow/app/model/director.rb”):
class Director < ActiveRecord::Base
 has_many :jobs
end
class Job < ActiveRecord::Basebelongs_to :directorend
@directors = Director.find(:all, :order => :name)
  def edit   @job = Job.find(params[:id])   @directors = Director.find(:all, :order => :name)  end Â
def new  @job = Job.new   @directors = Director.find(:all, :order => :name)   respond_to do |format|    format.html # new.html.erb    format.xml  { render :xml => @job }   end  end
<h1>Listing jobs</h1><table><tr><th>Name</th><th>Director</th></tr><% for job in @jobs %><tr><td><%=h job.name %></td><td><%=h job.director_id %></td><td><%= link_to ‘Show’, job %></td><td><%= link_to ‘Edit’, edit_job_path(job) %></td><td><%= link_to ‘Destroy’, job, :confirm => ‘Are you sure?’, :method => :delete %></td></tr><% end %></table><br /><%= link_to ‘New job’, new_job_path %>
<td><%=h job.director_id %></td>
<td><%=h job.director.name %></td>
<%= f.collection_select :director_id, @directors, :id, :name %>
<%= f.text_field :director_id %>
<h1>Editing job</h1><%= error_messages_for :job %><% form_for(@job) do |f| %><p><b>Name</b><br /><%= f.text_field :name %></p><p><b>Director</b><br /><%= f.collection_select :director_id, @directors, :id, :name %><%= f.text_field :director_id %></p><p><%= f.submit “Update” %></p><% end %><%= link_to ‘Show’, @job %> |<%= link_to ‘Back’, jobs_path %>
ruby script/server
localhost:3000/jobs
Tall, I think your problem with women is that you think you can solve any problems using terminal and applications 🙂