Thi*_*ent 7 activerecord ruby-on-rails has-many-through polymorphic-associations ruby-on-rails-4
在我的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
Run Code Online (Sandbox Code Playgroud)
我需要访问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)
Run Code Online (Sandbox Code Playgroud)
我的第一个猜测是添加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
Run Code Online (Sandbox Code Playgroud)
但是,取消的影响has_many :comments, through: posts,然后我再也无法访问comments是belong_to一个post从calendar的post belongs_to.
有没有办法兼顾 has_many :comments, through: posts 和 has_many :comments, through: ads工作?
-----
更新:根据这篇文章和Stack Overflow问题,答案可能在于使用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'
Run Code Online (Sandbox Code Playgroud)
-----
更新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?
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
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
Run Code Online (Sandbox Code Playgroud)
-----
更新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
Run Code Online (Sandbox Code Playgroud)
仍然没有工作,与上面相同的错误信息.
-----
更新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'
Run Code Online (Sandbox Code Playgroud)
现在,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
Run Code Online (Sandbox Code Playgroud)
还是行不通.
-----
更新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)
Run Code Online (Sandbox Code Playgroud)
我尝试了很多不同的东西,到目前为止我能想到的最好的是:
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
Run Code Online (Sandbox Code Playgroud)
然后,我按如下方式更新我的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
Run Code Online (Sandbox Code Playgroud)
但是这会返回以下错误:
NoMethodError at /calendars
undefined method `chain' for nil:NilClass
@posts_comments = @user.calendar_post_comments.order("created_at DESC").limit(5)
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
您应该尝试以下操作:
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'
Run Code Online (Sandbox Code Playgroud)
考虑到Ad和Post模型都具有以下特点:
has_many :comments, as: :commentable
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1747 次 |
| 最近记录: |