0
第一次海報 - 半新到Rails。這是我的問題。假設你有以下的多態多對許多協會的Rails:從ActiveRecord檢索和排序多個陣列聯接表 - Ruby on Rails
class Tag < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :books, :through => :taggings, :source => :taggable, :source_type => "Book"
has_many :movies, :through => :taggings, :source => :taggable, :source_type => "Movie"
end
class Tagging < ActiveRecord::Base
belongs_to :taggable, :polymorphic => true
belongs_to :tag
end
class Book < ActiveRecord::Base
has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings
end
class Movie < ActiveRecord::Base
has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings
end
現在我的問題是讓我們說我引用的Tagging數組回來,我需要採取taggable_id並返回所有書籍和電影作爲新陣列。我將如何做到這一點?
如果這是令人困惑的,從更一般的意義上說,如何找到給定標籤的所有電影和書籍?
我立即開始思考自定義SQL查找器是需要的,但我很好奇,如果我錯過了一些「導軌魔術」。 :)
謝謝!!