7

通過協會在我的Rails 4應用程序,我有以下型號:軌道4:結合的has_many:多態關聯

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 

我需要從calendaradbelongs_to訪問commentsbelong_to一個ad

這就是我想在我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效果然後我不能再訪問commentsbelong_topostcalendarpostbelongs_to

有沒有一種方法,使BOTHhas_many :comments, through: postshas_many :comments, through: ads工作?

-----

UPDATE:根據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 

-----

UPDATE 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 

仍然沒有工作,相同的錯誤如上述消息。

-----

UPDATE 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 

仍然沒有工作。

-----

UPDATE 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 

然後,我更新我的日曆控制器如下:

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) 

這裏有什麼問題?

+0

不能爲不同的關係,使用相同的關係名(你應該重命名' calendar_comments'到'post_comments'和'ad_comments')。另外,你可以刪除代碼中所有不相關的部分('has_many:invites'在這裏沒有用處) – MrYoshiji

回答

3

你應該嘗試以下操作:

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 
+0

好的,非常感謝。這對於User模型意味着什麼?我目前有'has_many:評論 has_many:calendar_comments,通過::日曆,:源=>:評論',但這不再工作。 –

+1

嘗試用'source_type:'評論'替換':source =>:comments'' – MrYoshiji

+0

完成。現在,我得到:'NoMethodError在/日曆未定義方法'post_comments'中爲#'。我想這是因爲我們沒有在'User'模型中聲明'post_comments'和'ad_comments',對吧? –