के सहयोग के माध्यम से अपने रेल में 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
मैं calendar
ad
belongs_to
से comments
कि belong_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
के प्रभाव को रद्द करता है और तब मैं अब comments
तक belong_to
calendar
post
से अब तक नहीं पहुंच सकता।
वहाँ दोनोंhas_many :comments, through: posts
औरhas_many :comments, through: ads
काम करने के लिए कोई तरीका है?
-----
अद्यतन: this article और that 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 => :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
फिर भी काम नहीं कर रहा।
-----
अद्यतन 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)
यहाँ क्या गलत है?
आप विभिन्न संबंधों के लिए एक ही संबंध नाम का उपयोग नहीं कर सकते हैं (आप नाम बदलना चाहिए ' calendar_comments' से 'post_comments' और' ad_comments')। साथ ही, आप अपने कोड के सभी अप्रासंगिक हिस्सों को हटा सकते हैं ('has_many: invites' यहां उपयोगी नहीं है) – MrYoshiji