嗨,我有一个多对多的关联,其中“帖子”有许多“感觉”,我想弄清楚如何找到用户的特定感觉的所有帖子。我的感觉模型有一个'name‘属性。
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
我试过了,但它显示我有错误的关联:"ActiveRecord::ConfigurationError:没有找到名为'feeling‘的关联;可能是您拼错了?“
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
发布于 2011-10-24 11:15:58
includes
参数应该是:feelings
-注意复数,这是您的关联的名称。
所以它应该是:
@user.posts.includes(:feelings)
https://stackoverflow.com/questions/7870119
复制相似问题