- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 Rails 4 应用程序中,我有以下模型:
User
has_many :administrations
has_many :calendars, through: :administrations
has_many :comments
has_many :calendar_comments, through: :calendars, :source => :comments
Calendar
has_many :administrations
has_many :users, through: :administrations
has_many :posts
has_many :comments, through: posts
has_many :ads
Administration
belongs_to :user
belongs_to :calendar
Post
belongs_to :calendar
has_many :comments, as: :commentable
Ad
belongs_to :calendar
has_many :comments, as: :commentable
Comment
belongs_to :commentable, polymorphic: true
belongs_to :user
comments
那个
belong_to
ad
来自
calendar
ad
belongs_to
.
Calendars#Index
中尝试做的事情行动:
@posts_comments = @user.calendar_comments.where(commentable_type: "Post").order("created_at DESC").limit(5)
@ads_comments = @user.calendar_comments.where(commentable_type: "Ad").order("created_at DESC").limit(5)
has_many :comments, through: ads
在日历模型中:
Calendar
has_many :administrations
has_many :users, through: :administrations
has_many :posts
has_many :comments, through: posts
has_many : ads
has_many :comments, through: ads
has_many :comments, through: posts
的效果然后我无法再访问
comments
那个
belong_to
post
来自
calendar
post
belongs_to
.
has_many :comments, through: posts
和
has_many :comments, through: ads
工作?
source:
和
source_type:
.
class Calendar < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :users, through: :administrations
has_many :posts, dependent: :destroy
has_many :commented_posts, through: :comments, source: :commentable, source_type: 'Post'
has_many :ads, dependent: :destroy
has_many :commented_ads, through: :comments, source: :commentable, source_type: 'Ad'
Could not find the source association(s) :comments in model Calendar. Try 'has_many :calendar_comments, :through => :calendars, :source => <name>'. Is it one of administrations, users, posts, commented_posts, ads, commented_ads, invites, or pokes?
class Calendar < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :users, through: :administrations
has_many :posts, dependent: :destroy
has_many :calendar_comments, :through => :calendars, :source => :post
has_many :ads, dependent: :destroy
has_many :calendar_comments, :through => :calendars, :source => :ad
class Calendar < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :users, through: :administrations
has_many :posts, dependent: :destroy
has_many :calendar_comments, :through => :commentable, :source => :post
has_many :ads, dependent: :destroy
has_many :calendar_comments, :through => :commentable, :source => :ad
class Calendar < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :users, through: :administrations
has_many :posts, dependent: :destroy
has_many :posts_comments, through: :posts, source_type: 'Post'
has_many :ads, dependent: :destroy
has_many :ads_comments, through: :ads, source_type: 'Ad'
has_many :calendar_comments, through: :calendars, :source => :comments
在
User
模型不再工作。
has_many :comments
has_many :calendar_post_comments, through: :calendars, :source => :post_comments
has_many :calendar_ad_comments, through: :calendars, :source => :ad_comments
@posts_comments = @user.calendar_comments.where(commentable_type: "Post").order("created_at DESC").limit(5)
@ads_comments = @user.calendar_comments.where(commentable_type: "Ad").order("created_at DESC").limit(5)
class Calendar < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :users, through: :administrations
has_many :posts, dependent: :destroy
has_many :post_comments, through: :posts, source_type: 'Post'
has_many :ads, dependent: :destroy
has_many :ad_comments, through: :ads, source_type: 'Ad'
end
class User < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :calendars, through: :administrations
has_many :comments
has_many :calendar_post_comments, through: :calendars, :source => :post_comments
has_many :calendar_ad_comments, through: :calendars, :source => :ad_comments
end
def index
@user = current_user
@calendars = @user.calendars.all
@posts_comments = @user.calendar_post_comments
.order("created_at DESC")
.limit(5)
@ads_comments = @user.calendar_ad.comments
.order("created_at DESC")
.limit(5)
end
NoMethodError at /calendars
undefined method `chain' for nil:NilClass
@posts_comments = @user.calendar_post_comments.order("created_at DESC").limit(5)
最佳答案
您应该尝试以下操作:
class Calendar < ActiveRecord::Base
# [ ... ]
has_many :posts
has_many :posts_comments, through: posts, source_type: 'Post'
has_many :ads
has_many :ads_comments, through: ads, source_type: 'Ad'
Ad
和
Post
两种型号都有以下特点:
has_many :comments, as: :commentable
关于ruby-on-rails - rails 4 : combining has_many :through association with polymorphic association,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34274737/
“多态”一词从何而来? 最佳答案 它来自希腊词根“poly”(许多)和“morphe”(形式)。多态对象可以采用多种形式(它可以由指向其任何祖先类的指针表示)。多态函数也可以有多种形式(它可以对实际上
我有一个名为 Frame 的基本结构这对一堆计算很有用:。 pub struct Frame { grid_val: Vec, grid_space: Vec, calcula
我有一个名为 Frame 的基本结构这对一堆计算很有用:。 pub struct Frame { grid_val: Vec, grid_space: Vec, calcula
“多态与方法重载或方法覆盖不同。......两者都不是......本身就是多态的实现”。 这是引自 wikipedia 然而,在“面向对象编程”一书中,Timothy Budd 指出有“四种不同形式的
我有一组我没有编写的类,它们是只读的。例如,假设这些类是以下类: public class Base { } public class B : Base { } public class C : B
什么是 Smalltalk 中的“无限动态多态性”?有人可以举个例子吗? 在这个 book 中提到了它:C++ 模板:完整指南,p. 238. 最佳答案 See在 C++ 中:通过继承实现的多态性是有
我在 Laravel 4 中有一个多态关系,它像我想要的那样工作。我有另一个,当我尝试插入相关模型时它不起作用,即使它与第一个相同并且我以相同的方式使用它。 我有 Event模型和 Article模型
我试图弄清楚如何在 Hack 中实现访客模式。它显然需要函数重载多态性,但正如我所测试的,这个例子: visitInt($m); } else if (is_string($m)) {
两者有什么区别? 具有 myMethod(int a) 的父类(super class)以及具有相同方法的继承类, 这是覆盖还是多态? 我很清楚黑白覆盖和重载的区别,但多态性和覆盖似乎是一样的。还是他
来自 Programming Languages: Principles and Paradigms, byMaurizio Gabbrielli, Simone Martini Definition
给定这段代码 locale A = fixes foo :: "'a" locale B = A + fixes bar :: "'a × 'a" locale C' = A + fixe
榆树市可能存在以下情况? func : a -> {a | id : Int} func x = { x | id = 123 } 由于a的多态性太强,因此无法编译。它认为可以是任何东西,包括非记录类
我只想有一个在 Hashtbls 上通用的简单函数,所以我写了这个: let iter_htbl (type a) (module H : Hashtbl.S with type key = a) h
我了解Polymorphic 和Metamorphic 代码的概念,但是最近我同时阅读了两者的Wikipedia页面(出于某种原因,我以前没有这样做过!)。现在,我真的很想为自己编写一些变形代码。 我
多态性是过载的另一个术语吗? 最佳答案 没有;重载是创建一个具有相同名称,带有不同数量参数或具有另一种类型参数的方法。 多态性是指在各种类型(都具有相同的“基本类型”)中更改特定方法的实现/功能。 重
我试图在 OCaml 中表示一组语法的产生式,存在类型对于建模语法规则的语义 Action 非常有用。我一直在研究 Menhir 源代码,存在类型也用于建模语义 Action 。考虑以下因素: typ
我在SML的学习上取得了一些进步,试图巩固我对一些最基本概念的理解。下面的练习证明还有很多东西要学。 我被要求实现一个名为 add 的函数,它接收两个数字并返回它们的总和。问题是,add 应该接收整数
首先,请看这些 Java 代码: Drawable.java package examples.simple.model; public interface Drawable { public
考虑以下代码: trait Animal { fn make_sound(&self) -> String; } struct Cat; impl Animal for Cat { f
考虑以下代码: trait Animal { fn make_sound(&self) -> String; } struct Cat; impl Animal for Cat { f
我是一名优秀的程序员,十分优秀!