Im completely new to rails and have trying to get my head around activerecord.
I have these ruby objects
With a mysql database to reflects this.
class Client < ActiveRecord::Base
class ClientOrganisation < ActiveRecord::Base
what im trying to do is
@client = Client.find(params[:id])
@client.client_organisation #this returns nil
@client.client_organisation_id #this returns the right foreign key id
Now from my understand @client.client_organisation should return a ClientOrganisation object. Is there a method to make the @client load its relational objects? Is there anything else I am missing, off track on?
Thanks you the help
I'm not sure why, it should autoload that, but you could add this to the find:
@client = Client.find(params[:id], :include => :client_organisation)
It should definately be loading it automatically - adding the include statement just tells Rails to load the associated objects with one query using JOINS for a speed increase.
I can't see any immediate problem with your code, can you post your schema?
Thanks for your help.
What I had wrong was the foreign key was client_organisations_id when it should have been client_organisation_id. Looking at the example I gave I got it right there :blush: but not in my live code.