Hi I have a many to many association where ‘posts’ have many ‘feeling’, I’d like to figure out how to find all the posts with a specific feeling by the user. My Feeling model has a ‘name’ attribute.
class User < ActiveRecord::Base
has_many :posts, :dependent => :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
class Post < ActiveRecord::Base
has_many :feelingships
has_many :feelings, :through => :feelingships
belongs_to :user
end
class Feeling < ActiveRecord::Base
has_many :feelingships
has_many :posts, :through => :feelingships
end
class Feelingship < ActiveRecord::Base
belongs_to :post
belongs_to :feeling
attr_accessible :post_id, :feeling_id
end
I tried this but it says I have the wrong association: “ActiveRecord::ConfigurationError: Association named ‘feeling’ was not found; perhaps you misspelled it?”
def feeling
@user = User.find(params[:id])
@feed_items= @user.posts.includes(:feeling).where(
['`feelings`.name = ?', params[:feeling]])
@feed_items = @feed_items.paginate(:per_page => "10", :page => params[:page])
render 'shared/_feed', :layout => 'head_layout'
end
The
includesargument should be:feelings– notice the plural, which is what your association is named.So it should be: