gpt4 book ai didi

ruby-on-rails - rails 4 : combining has_many :through association with polymorphic association

转载 作者:行者123 更新时间:2023-12-04 05:45:46 29 4
gpt4 key购买 nike

在我的 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工作?

————

更新 :根据 this articlethat Stack Overflow question ,答案可能在于使用 source:source_type: .

但是不确定如何使用这些。

————

更新 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'

————

更新 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?

我尝试了以下方法:
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

————

更新 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

仍然无法正常工作,与上述相同的错误消息。

————

更新 5 : 基于 MrYoshiji 的答案,我现在有
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 => :commentsUser模型不再工作。

我试过:
has_many :comments
has_many :calendar_post_comments, through: :calendars, :source => :post_comments
has_many :calendar_ad_comments, through: :calendars, :source => :ad_comments

还是行不通。

————

更新 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)

我尝试了很多不同的东西,到目前为止我能想到的最好的是:
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

然后,我更新我的日历 Controller 如下:
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'

考虑到 AdPost两种型号都有以下特点:
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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com