Topic: Has_many :through errors

Okay, I've worked it out.  It was because I put :tasks and :documents in one line.  Figured I'd leave this up in case anyone makes the same mistake.




I've converted one of my HABTM associations to has_many :through.  With lots of code missed out here's what I've got:

class Document < ActiveRecord::Base
  has_many :tasks, :through => :resourceds
end

class Task < ActiveRecord::Base
  has_many :documents, :through => :resourceds
end

class Resourced < ActiveRecord::Base
  has_many :tasks, :documents
end


I've tried running my old unit tests to check I haven't broken too much and I'm getting a strange error:

LoadError: Expected *pathtomodelsdirectory*/resourced.rb to define Resourced.

But resourced.rb exists.

Last edited by Draconid (2007-01-29 10:19:02)

Re: Has_many :through errors

You probably want the belongs_to association as well:

class Resourced < ActiveRecord::Base
  belongs_to :task
  belongs_to :document
end

Railscasts - Free Ruby on Rails Screencasts

Re: Has_many :through errors

Thanks.  That fixes the error I was just about to post about, but now I'm getting another error!

changing that gives me an "undefined method 'table_name' for Tasks:Module" when doing

d.tasks << @t

(where t is the task and d is the document).  Can I not use this method for adding a task to a document anymore?

I wish I'd started with has_many :through rather than changing partway through - trying to change from one method to another is such a pain!

Re: Has_many :through errors

Draconid wrote:

changing that gives me an "undefined method 'table_name' for Tasks:Module" when doing

d.tasks << @t

(where t is the task and d is the document).  Can I not use this method for adding a task to a document anymore?

This should work in Rails 1.2, which version are you using?

However, the thinking behind has_many :through is very different. Here you are making the join an actual model (Resourced) with its own attributes. Instead of thinking about adding a task to a document, think of making a resource.

Resourced.create(:task => @t, :document => d)

Draconid wrote:

I wish I'd started with has_many :through rather than changing partway through - trying to change from one method to another is such a pain!

I know what you mean. I wish there was an easier way.

Railscasts - Free Ruby on Rails Screencasts

Re: Has_many :through errors

Well, now I get a different error!  "underfined method 'task=' for #<Resourced:0x46659c8>"

I'm tempted to start with these two models from scratch, see if I can work this out from the beginning.

As for the version of Rails I'm using InstantRails on this machine, but on my other machine (I'm using svn to get the code between two different machines depending on where I'm working) I'm possibly running something different.  I did a rake:freeze:edge (on the other machine) so that I can use the RESTful routing stuff so that's in my vendor directory - so I assume it should use that version?  I should probably see about making sure I have 1.2 on both machines I suppose...

I don't suppose you know of any good tutorial for transfering from habtm to has_many :through?

Re: Has_many :through errors

Cracked it!  I'd done belongs_to :tasks rather than belongs_to :task.

Thanks for the help!

Re: Has_many :through errors

Draconid wrote:

I don't suppose you know of any good tutorial for transfering from habtm to has_many :through?

I don't know of any, but that's a good idea. I might write one sometime.

Railscasts - Free Ruby on Rails Screencasts