NoMethodError when using .where (eager fetching)

Posted by Ethan Leroy on Stack Overflow See other posts from Stack Overflow or by Ethan Leroy
Published on 2012-10-31T22:58:16Z Indexed on 2012/10/31 23:00 UTC
Read the original article Hit count: 135

Filed under:
|
|

I have the following model classes...

class Image < ActiveRecord::Base
  attr_accessible :description, :title
  has_many :imageTags
  has_many :tags, :through => :imageTags
end

class Tag < ActiveRecord::Base
  attr_accessible :name
  has_many :imageTags
  has_many :images, :through => :imageTags
end

class ImageTag < ActiveRecord::Base
  attr_accessible :position
  belongs_to :image
  belongs_to :tag
end

And when I use find for getting the Tag with the id 1

t = Tag.find(1);
@images = t.images;

But when I do the same with where, I get a NoMethodError, with the description undefined method 'images':

t = Tag.where(:name => "foo");
@images = t.images;

I also tried adding .includes(:images) before the .where statement, but that doesn't work too. So, how can I get all Images that belong to a Tag?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby