首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails 3:调用has_many关联上的数据库

Rails 3:调用has_many关联上的数据库
EN

Stack Overflow用户
提问于 2011-10-24 07:46:17
回答 1查看 114关注 0票数 0

嗨,我有一个多对多的关联,其中“帖子”有许多“感觉”,我想弄清楚如何找到用户的特定感觉的所有帖子。我的感觉模型有一个'name‘属性。

代码语言:javascript
运行
复制
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‘的关联;可能是您拼错了?“

代码语言:javascript
运行
复制
 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-24 11:15:58

includes参数应该是:feelings -注意复数,这是您的关联的名称。

所以它应该是:

代码语言:javascript
运行
复制
@user.posts.includes(:feelings)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7870119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档