0
我有一個使用Ramaze和Sequel的紅寶石應用程序。續集加入:「.id」返回另一個表的ID
我有類標籤和標記具有這種關係:
class Tag < Sequel::Model
one_to_many :taggings, :class => 'Thoth::Tagging'
class Tagging < Sequel::Model(:taggings)
many_to_one :tag, :class => 'Thoth::Tag'
我想返回的標籤列表中普及的順序,即有最引用的Tagging的那些(過濾掉任何有少於三個標籤)。我做了這樣:
tags = .left_outer_join(:taggings, :tag_id => :id).group(:tag_id).having("count(*) > 2").order("count(*) desc").all
這不會返回什麼似乎是一些標籤的對象,但是當我呼籲他們.ID,我得到一個指向標籤的標籤的ID,而不是標籤本身。
在仔細檢查,結果是從一個普通的發現完全不同的:
> tag_regular = Tag[2]
=> #<Thoth::Tag @values={:title=>nil, :position=>nil, :parent_id=>1, :name=>"academic", :id=>2}>
> tag_from_join = Tag.join(:taggings, :tag_id => :id).group(:tag_id).having("count(*) > 2").order("count(*) desc").all.select{|tag| tag.name == "academic"}.first
=> #<Thoth::Tag @values={:tag_id=>2, :post_id=>5, :title=>nil, :position=>nil, :parent_id=>1, :name=>"academic", :id=>1611, :created_at=>nil}>
在這兩種情況下,我得到一個透特::標籤,但值有很大的不同,基於不同的領域加入我想。
我實際上需要做的是獲得按標籤數排序的常規標籤對象列表,但採用高效的單一查詢方式。有沒有更好的辦法?
感謝,最大