[英]Rails 4: combining has_many :through association with polymorphic association
In my Rails 4 app, I have the following models: 在我的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
I need to access the comments
that belong_to
an ad
from the calendar
the ad
belongs_to
. 我需要访问comments
认为belong_to
的ad
从calendar
的ad
belongs_to
。
This is what I am trying to do in my Calendars#Index
action: 这就是我在我的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)
My first guess was to add has_many :comments, through: ads
in the Calendar model: 我的第一个猜测是添加has_many :comments, through: ads
日历模型中的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
But that cancels the effect of has_many :comments, through: posts
and then I can no longer access the comments
that belong_to
an post
from the calendar
the post
belongs_to
. 但是,取消的影响has_many :comments, through: posts
,然后我再也无法访问comments
认为belong_to
的post
从calendar
中post
belongs_to
。
Is there a way to make BOTH has_many :comments, through: posts
AND has_many :comments, through: ads
work? 有没有办法让两个人 has_many :comments, through: posts
和 has_many :comments, through: ads
有效吗?
————— -----
UPDATE : according to this article and that Stack Overflow question , the answer may lay in the use of source:
and source_type:
. 更新 :根据这篇文章和Stack Overflow问题 ,答案可能在于使用source:
和source_type:
.
Not sure how to use those however. 但不确定如何使用它们。
————— -----
UPDATE 2 : would the following code make any sense? 更新2 :以下代码是否有意义?
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'
————— -----
UPDATE 3 : when I try the above code, I get the following error message: 更新3 :当我尝试上面的代码时,我收到以下错误消息:
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?
I tried the following: 我尝试了以下方法:
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
————— -----
UPDATE 4 : a new failed attempt with the following code: 更新4 :使用以下代码的新尝试失败:
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
Still not working, same error message as above. 仍然没有工作,与上面相同的错误信息。
————— -----
UPDATE 5 : based on MrYoshiji 's answer, I now have 更新5 :根据Mr.Yoshiji的回答,我现在有了
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'
Now, has_many :calendar_comments, through: :calendars, :source => :comments
in the User
model is no longer working. 现在, has_many :calendar_comments, through: :calendars, :source => :comments
User
模型中的has_many :calendar_comments, through: :calendars, :source => :comments
不再有效。
I tried: 我试过了:
has_many :comments
has_many :calendar_post_comments, through: :calendars, :source => :post_comments
has_many :calendar_ad_comments, through: :calendars, :source => :ad_comments
Still not working. 还是行不通。
————— -----
UPDATE 6 : I am still stuck with this issue, since I cannot figure out a way to get the following code working: 更新6 :我仍然坚持这个问题,因为我无法弄清楚如何使以下代码工作:
@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)
I tried many different things, and the best I could come up with so far was: 我尝试了很多不同的东西,到目前为止我能想到的最好的是:
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
Then, I update my Calendars controller as follows: 然后,我按如下方式更新我的Calendars控制器:
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
But this returns the following error: 但是这会返回以下错误:
NoMethodError at /calendars
undefined method `chain' for nil:NilClass
@posts_comments = @user.calendar_post_comments.order("created_at DESC").limit(5)
What is wrong here? 这有什么不对?
You should try the following: 您应该尝试以下方法:
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'
Considering that Ad
and Post
models both have the following: 考虑到Ad
和Post
模型都具有以下特征:
has_many :comments, as: :commentable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.