我在編寫和測試涉及幾個連接和關聯的範圍時遇到了問題。我會盡量保持我的解釋簡潔,但儘可能徹底。如何在Rails中使用關聯設置此範圍?
我有以下的關聯:
ExpertTopic > Topic > Articles > Posts
和下面的代碼:
class Topic < ActiveRecord::Base
has_many :articles, :order => "position", :dependent => :destroy
has_many :posts, :through => :articles
has_many :expert_topic, :dependent => :delete_all
has_many :experts, :through => :expert_topic
end
和:
class ExpertTopic < ActiveRecord::Base
belongs_to :topic, :inverse_of => :expert_topic
belongs_to :expert, :inverse_of => :expert_topic
scope :live, joins(:topic => {:articles => :post})
.where("topics.article_count > ? AND posts.live = ?", 0, true)
end
隨着的live
範圍,我正在努力縮小與專題相關的專家的所有實時文章(通過文章)。
在Rails的控制檯ExpertTopic.live.to_sql
是:
"SELECT `experts_topics`.* FROM `experts_topics` INNER JOIN
`topics` ON `topics`.`id` = `experts_topics`.`topic_id` INNER JOIN
`articles` ON `articles`.`topic_id` = `topics`.`id` INNER JOIN
`posts` ON `posts`.`id` = `articles`.`post_id` WHERE
(topics.article_count > 0 AND posts.live = 1)"
我測試在expert_topic_spec.rb
我用下面的代碼範圍:
describe ExpertTopic do
before do
@post1 = FactoryGirl.create(:pending_post)
@post2 = FactoryGirl.create(:live_post)
@post3 = FactoryGirl.create(:pending_post)
@post4 = FactoryGirl.create(:live_post)
@non_live_topic = FactoryGirl.create(:topic_with_posts, :posts => [@post1, @post2, @post3])
@live_topic = FactoryGirl.create(:topic_with_posts, :posts => [@post2, @post4])
FactoryGirl.create(:expert_topic, topic_id: @non_live_topic.id)
FactoryGirl.create(:expert_topic, topic_id: @live_topic.id)
end
it 'finds and returns only expert with live topic' do
ExpertTopic.all.count.should == 2
ExpertTopic.live.uniq.count.should == 1
end
end
的邏輯是,既然@non_live_topic
包含至少一個帖子不是直播,它不被視爲直播,因此不應通過致電ExpertTopic.live
返回。但是,最後的斷言失敗,因爲ExpertTopic.live.uniq.count
返回2
而不是1
。
我不知道我的範圍寫錯了,還是我的測試,我真的很感謝有人幫助調試!
謝謝!
這正是我所需要的。我認爲我的邏輯可能存在缺陷,但我需要比我更聰明的人來指出它是什麼。非常感謝!如果我能提供這樣一個有用的答案,我會給你兩張票。 – pthesis