Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 多态关联问题_Ruby On Rails_Ruby On Rails 3_Polymorphism - Fatal编程技术网

Ruby on rails 多态关联问题

Ruby on rails 多态关联问题,ruby-on-rails,ruby-on-rails-3,polymorphism,Ruby On Rails,Ruby On Rails 3,Polymorphism,在我的Rails应用程序中,我的模型如下所示: class Blog < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :blog end class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end classblog

在我的Rails应用程序中,我的模型如下所示:

class Blog < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :blog
end

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
end
classblogtrue
终止
我现在的问题是在一个特定的博客下找到所有的评论。有人能找到解决办法吗

致以最良好的祝愿,
埃里克

class Blog < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :blog
  has_many :comments, :as=>:commentable #Post.first.comments or Blog.posts.first.comments
end

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
end

你是如何找到评论的?你能帮我们发那个密码吗?如果您在控制台中收到错误消息,那么错误消息是什么?我不知道您为什么要使用多态关联?您好!我正在使用多态关联,因为将来我想允许对帖子以外的其他对象进行评论……谢谢,我知道这个解决方案。但问题是,这需要我首先获取所有帖子,然后循环浏览所有帖子。我宁愿找到一种方法,我可以通过SQL获取它们…
comments=[]
Blog.first.posts.each do |post|
  comments = comments | post.comments
end