Ruby on rails 有多个:到,:source,:source类型返回空数组
我得到了一些Ruby on rails 有多个:到,:source,:source类型返回空数组,ruby-on-rails,model-view-controller,has-many-through,has-many,has-many-polymorphs,Ruby On Rails,Model View Controller,Has Many Through,Has Many,Has Many Polymorphs,我得到了一些Manager和SoccerTeam模型。一个经理“拥有”许多足球队;经理也可以评论足球队,也可以评论其他经理: manager.rb # Soccer teams the manager owns has_many :soccer_teams, :dependent => :restrict # Comments the manager has made on soccer teams or other managers has_many :reviews, :class_n
Manager
和SoccerTeam
模型。一个经理“拥有”许多足球队;经理也可以评论足球队,也可以评论其他经理:
manager.rb
# Soccer teams the manager owns
has_many :soccer_teams, :dependent => :restrict
# Comments the manager has made on soccer teams or other managers
has_many :reviews, :class_name => "Comment", :foreign_key => :author_id, :dependent => :destroy
# Comments the manager has received by other managers
has_many :comments, :as => :commentable, :dependent => :destroy
# Soccer teams that have received a comment by the manager
has_many :observed_teams, :through => :comments, :source => :commentable, :source_type => "SoccerTeam"
# The manager that owns the team
belongs_to :manager
# Comments received by managers
has_many :comments, :as => :commentable, :dependent => :destroy
# Managers that have reviewed the team
has_many :observers, :through => :comments, :source => :author, :class_name => "Manager"
belongs_to :commentable, :polymorphic => true
belongs_to :author, :class_name => Manager
足球队.rb
# Soccer teams the manager owns
has_many :soccer_teams, :dependent => :restrict
# Comments the manager has made on soccer teams or other managers
has_many :reviews, :class_name => "Comment", :foreign_key => :author_id, :dependent => :destroy
# Comments the manager has received by other managers
has_many :comments, :as => :commentable, :dependent => :destroy
# Soccer teams that have received a comment by the manager
has_many :observed_teams, :through => :comments, :source => :commentable, :source_type => "SoccerTeam"
# The manager that owns the team
belongs_to :manager
# Comments received by managers
has_many :comments, :as => :commentable, :dependent => :destroy
# Managers that have reviewed the team
has_many :observers, :through => :comments, :source => :author, :class_name => "Manager"
belongs_to :commentable, :polymorphic => true
belongs_to :author, :class_name => Manager
comment.rb
# Soccer teams the manager owns
has_many :soccer_teams, :dependent => :restrict
# Comments the manager has made on soccer teams or other managers
has_many :reviews, :class_name => "Comment", :foreign_key => :author_id, :dependent => :destroy
# Comments the manager has received by other managers
has_many :comments, :as => :commentable, :dependent => :destroy
# Soccer teams that have received a comment by the manager
has_many :observed_teams, :through => :comments, :source => :commentable, :source_type => "SoccerTeam"
# The manager that owns the team
belongs_to :manager
# Comments received by managers
has_many :comments, :as => :commentable, :dependent => :destroy
# Managers that have reviewed the team
has_many :observers, :through => :comments, :source => :author, :class_name => "Manager"
belongs_to :commentable, :polymorphic => true
belongs_to :author, :class_name => Manager
现在,如果我有一位经理对SoccerTeam发表评论,我希望能发现:
和manager.reviews
soccer\u团队中的
对象。commentsComment
管理者中的对象。观察团队SoccerTeam
- 足球队中的
对象。观察员管理者
经理时,我总是得到一个空数组。要实际获得一位经理评论的足球队名单,我需要使用:
manager.reviews.collect{ |review| Kernel.const_get(review.commentable_type).find(review.commentable_id) if review.commentable_type == "SoccerTeam" }
这很难看。我希望简单的经理。观察到的团队能够工作……为什么不能
编辑
我进一步理解了为什么它不起作用。事实上,生成的SQL是:
SELECT "soccer_teams".* FROM "soccer_teams" INNER JOIN "comments" ON "soccer_teams".id = "soccer_teams".commentable_id AND "comments".commentable_type = 'SoccerTeam' WHERE (("comments".commentable_id = 1) AND ("comments".commentable_type = 'Manager'))
虽然我希望它是:
SELECT "soccer_teams".* FROM "soccer_teams" INNER JOIN "comments" ON "soccer_teams".id = "comments".commentable_id AND "comments".commentable_type = 'SoccerTeam' WHERE ("comments".author_id = 1)
所以问题很简单:如何获得该查询?(对:外键ans:as
的启发式尝试,正如预期的那样,并没有解决问题!)我认为您只是对观察到的团队使用了错误的关联。而不是
has_many :observed_teams, :through => :comments,
:source => :commentable, :source_type => "SoccerTeam"
试试这个:
has_many :observed_teams, :through => :reviews,
:source => :commentable, :source_type => "SoccerTeam"
也在,
has_many :reviews, :class_name => :comment,
:foreign_key => :author_id, :dependent => :destroy
:comment
应该是“comment”
而且
has_many :comments, :as => commentable, :dependent => :destroy
commmentable
应该是:commmentable
最后两个是打字错误……但你肯定明白了关联的意义!谢谢