- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我得到了一些 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
Comment
对象在 manager.reviews
并在 soccer_team.comments
SoccerTeam
对象在 manager.observed_teams
Manager
对象在 soccer_team.observers
manager.observed_teams
时我总是获得一个空数组。要实际获得经理评论的足球队名单,我需要使用:
manager.reviews.collect{ |review| Kernel.const_get(review.commentable_type).find(review.commentable_id) if review.commentable_type == "SoccerTeam" }
manager.observed_teams
工作……为什么不工作?
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)
:foreign_key
ans
:as
进行启发式尝试并没有解决问题!)。
最佳答案
我认为您只是对 observed_teams
使用了错误的关联.代替
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
关于ruby-on-rails - has_many :through, :source, :source_type 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5052588/
我的模型目前低于。 user.rb class User < ActiveRecord::Base has_many :authentications end authentication.rb
出于某种原因,多态的源类型 has_many :through尽管设置了 :source_type,但关联始终为 0。 . 这是我的模型的样子...... 福: has_many :tagged_it
我得到了一些 Manager和 SoccerTeam模型。一名经理“拥有”许多足球队;经理也可以评论足球队,也可以评论其他经理: manager.rb # Soccer teams the manag
我有三个模型:用户、组织和角色。一个用户通过一个角色可以访问多个组织,一个组织可以通过其角色拥有多个用户。另外,用户可以通过角色访问其他模型,所以角色模型有一个名为“access_to”的多态belo
所以这是一个示例类 class Company { :investors => { :type => 'VcFirm'} } has_many :angels, through: :inve
我是一名优秀的程序员,十分优秀!