Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.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_Polymorphic Associations_Has Many Polymorphs - Fatal编程技术网

Ruby on rails 中的冲突关联有许多多形

Ruby on rails 中的冲突关联有许多多形,ruby-on-rails,ruby-on-rails-3,polymorphism,polymorphic-associations,has-many-polymorphs,Ruby On Rails,Ruby On Rails 3,Polymorphism,Polymorphic Associations,Has Many Polymorphs,我正在使用has_many_polymorphs在一个网站上创建一个“收藏夹”功能,在这里多个用户可以发布故事和发表评论。我希望用户能够“喜爱”的故事和评论 class User < ActiveRecord::Base has_many :stories has_many :comments has_many_polymorphs :favorites, :from => [:stories, :comments] end class Story < ActiveR

我正在使用has_many_polymorphs在一个网站上创建一个“收藏夹”功能,在这里多个用户可以发布故事和发表评论。我希望用户能够“喜爱”的故事和评论

class User < ActiveRecord::Base
 has_many :stories
 has_many :comments

 has_many_polymorphs :favorites, :from => [:stories, :comments]
end

class Story < ActiveRecord::Base
  belongs_to :user, :counter_cache => true
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :user, :counter_cache => true
  belongs_to :story, :counter_cache => true
end

class FavoritesUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :favorite, :polymorphic => true
end
我在一篇文章中找到了关于这个错误的讨论,但它没有回答我的问题


这是怎么回事?我怎样才能做得更好呢?

像这样的东西怎么样

class UserFavorite < ActiveRecord::Base
  belongs_to :user
  belongs_to :favorite, :polymorphic => true
end

class User < ActiveRecord::Base
  has_many :favourite_story_items, :class_name => "UserFavourite", :conditions => "type = 'Story'"
  has_many :favourite_stories, :through => :favourite_story_items, :as => :favourite
  has_many :favourite_comment_items, :class_name => "UserFavourite", :conditions => "type = 'Comment'"
  has_many :favourite_comments, :through => :favourite_comment_items, :as => :favourite
end
class-UserFavoritetrue
结束
类用户“userfavorite”,:conditions=>“type='story'”
有很多:最喜欢的故事,:至=>:最喜欢的故事项目,:as=>:最喜欢的
有很多:收藏夹注释项,:class\u name=>“userfavorite”,:conditions=>“type='comment'”
有很多:收藏夹注释,:至=>:收藏夹注释项目,:as=>:收藏夹
结束

我在这里遇到了同样的问题:你最后做了什么?
class FavoriteStory < Story
end

class FavoriteComment < Comment
end
Could not find a valid class for :favorite_comments (tried FavoriteComment). If it's namespaced, be sure to specify it as :"module/favorite_comments" instead.
class UserFavorite < ActiveRecord::Base
  belongs_to :user
  belongs_to :favorite, :polymorphic => true
end

class User < ActiveRecord::Base
  has_many :favourite_story_items, :class_name => "UserFavourite", :conditions => "type = 'Story'"
  has_many :favourite_stories, :through => :favourite_story_items, :as => :favourite
  has_many :favourite_comment_items, :class_name => "UserFavourite", :conditions => "type = 'Comment'"
  has_many :favourite_comments, :through => :favourite_comment_items, :as => :favourite
end