嗨我有很多关联,其中'帖子'有很多'感觉',我想知道如何找到具有用户特定感觉的所有帖子。我的感觉模型有一个'名字'属性。
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:没有找到名为'感觉'的关联;也许你拼错了它?”
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
答案 0 :(得分:1)
includes
参数应为:feelings
- 注意复数,即你的关联名称。
所以它应该是:
@user.posts.includes(:feelings)