afxjzs
afxjzs

Reputation: 982

Rails 3 'association :[...] not found' error for has_many :through relationship

I'm pretty sure I have my models set up correctly for a standard has_many :through relationship...however, I used a rails generator for a model to create it and the first time i ran it, i accidentally created a model called Categorizations (plural) instead of Categorization and went all the way through setting everything up before i realized the error. I rolled back the migration and deleted the 4 files created by the generator (migration, model, spec, factory). I started over again and i'm still getting Association :category not found as the error.

my main question: Is there anything else I need to undo because I accidentally created and deleted a model with a plural name?

Followup question: If that's not the issue, then what am I doing wrong? Is there some glaring oversight because i've been staring at this so long?

Follow up to the follow up: What else can I do to help troubleshoot this issue? All of the models are accessible in the console and seem to be set up correctly. is there a rake routes for model associations?

Here is what i have in my view:

<%= f.association :category %>

I've also tried:

<%= f.association :category, :required => true, :collection => @categories, :as => :check_boxes, :label => "Categories:" %> (I have @categories set in the controller

Here are the models:

class PressRelease < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, :through => :categorizations

  belongs_to :user
  belongs_to :press_contact

  attr_accessible :body, :summary, :user_id, :headline, :link, :press_contact_id, :publish_date, :subheadline

  self.per_page = 5

  def published
    "#{self.publish_date.strftime("%A, %B %d, %Y")}"
  end

end

class Category < ActiveRecord::Base
  has_many :categorizations
  has_many :press_releases, :through => :categorizations
  attr_accessible :description, :label
end

class Categorization < ActiveRecord::Base
  belongs_to :category
  belongs_to :press_release

  attr_accessible :category_id, :press_release_id

end

update: This all works in the console:

pr = PressRelease.first
pr.categories
Category Load (0.2ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."press_release_id" = 1

c = Category.first
c.press_releases
PressRelease Load (0.3ms)  SELECT "press_releases".* FROM "press_releases" INNER JOIN "categorizations" ON "press_releases"."id" = "categorizations"."press_release_id" WHERE "categorizations"."category_id" = 21
 => [] 

So the association seems to be working on some level. i'm getting more and more confident that i'm missing something little and obvious or giant and glaring. please help! thanks!

Upvotes: 0

Views: 555

Answers (1)

pablomarti
pablomarti

Reputation: 2107

If you are working over PressRelease you should use <%= f.association :categories %>, change the :category for :categories, because of the relation.

Upvotes: 1

Related Questions