나는 주석을 달고 평가할 수있는 "내용"과 "프로파일"이라는 두 개의 표가 있습니다. 나는 다형성 협회를 사용하여 보았고 그것에 반대했다. 다형성 연결을 사용하려면 테이블 "등급"과 "주석"이 모두이 기능을 갖습니다.여러 콘크리트 수퍼 테이블로이를 수행 할 수 있습니까?
구체적인 수퍼 퍼블리시 구현으로 이것이 가능합니까? 그렇다면 어떻게해야할까요?
나는 주석을 달고 평가할 수있는 "내용"과 "프로파일"이라는 두 개의 표가 있습니다. 나는 다형성 협회를 사용하여 보았고 그것에 반대했다. 다형성 연결을 사용하려면 테이블 "등급"과 "주석"이 모두이 기능을 갖습니다.여러 콘크리트 수퍼 테이블로이를 수행 할 수 있습니까?
구체적인 수퍼 퍼블리시 구현으로 이것이 가능합니까? 그렇다면 어떻게해야할까요?
이 시도 :
class Commentable < ActiveRecord::Base
has_many :comments
has_one :profile, :content
end
class Comment < ActiveRecord::Base
belongs_to :commentable
end
class Content < ActiveRecord::Base
belongs_to :commentable
end
class Profile < ActiveRecord::Base
belongs_to :commentable
end
이 (스크립트/콘솔) 할 수 있습니다 :
# Profile.rb
# instead of doing a has many through,
# we can just use instance method with custom join.
def comments
find(self.id, :joins => "JOIN commentables cm ON (profiles.id = cm.id)
LEFT OUTER JOIN comments c ON (cm.id = c.commentable_id)")
end
:
# every time you create a profile, link it to the
# commentable row associated to this profile instance
cid = Commentable.create().id
p = Profile.create(:name => "Inspector Gadget",
:commentable_id => cid)
# when creating the comment, assign the commentable_id
# to link it to a Profile comment
Comment.new do |c|
c.body = "go go gadget"
c.email = "[email protected]"
c.commentable_id = p.commentable_id
c.save!
end
사용, 프로필에서이 트릭을 데이터베이스에서 의견을 retreive하려면
테스트되지 않은 코드!
는 Why can you not have a foreign key in a polymorphic association?
당신은 조인에 열을 혼합하고, 자세한 내용과 explaination 여기를 참조하십시오. 그것은이 있어야한다 :
find(self.id, :joins => "JOIN commentables cm ON (profiles.commentable_id = cm.id)
LEFT OUTER JOIN comments c ON (cm.id = c.commentable_id)")
내가 틀렸다,하지만 당신은 단지
def comments
Comments.find(:all, :conditions => ["commentable_id = ?", self.commentable_id])
end
Profiles.rb
에서이 작업을 수행 할 수없는 경우 나 수정