For several days Iβve been trying to understand what a seam should be very light ... Iβm still very new to the world of rails and rubies, and I just canβt work on this one of ...: p
In any case, the problem I am facing is that I have several columns: counter_cache in my model, which all work very well when checking them manually. However, I want to do TDD and I cant the seam to check them in rspec for an unknown reason ??
Anyway, here is an example of my model (User's, comments and Media):
class User < ActiveRecord::Base has_many :comments has_many :media, dependent: :destroy end class Comment < ActiveRecord::Base attr_accessible :content, :user_id belongs_to :commentable, polymorphic: true, :counter_cache => true belongs_to :user, :counter_cache => true validates :user_id, :presence => true validates :content, :presence => true, :length => { :maximum => 255 } end class Medium < ActiveRecord::Base attr_accessible :caption, :user_id belongs_to :user, :counter_cache => true has_many :comments, as: :commentable validates :user_id, presence: true validates :caption, length: { maximum: 140 }, allow_nil: true, allow_blank: true default_scope order: 'media.created_at DESC' end
Here is an example of setting a table schema:
create_table "users", :force => true do |t| t.integer "comments_count", :default => 0, :null => false t.integer "media_count", :default => 0, :null => false end create_table "comments", :force => true do |t| t.text "content" t.integer "commentable_id" t.string "commentable_type" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.integer "user_id" end create_table "media", :force => true do |t| t.integer "user_id" t.string "caption" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.integer "comments_count", :default => 0, :null => false end
And here is an example rspec example that I tried:
require 'spec_helper' describe "Experimental" do describe "counter_cache" do let!(:user) { FactoryGirl.create(:user)} subject { user } before do @media = user.media.create(caption: "Test media") end its "media array should include the media object" do m = user.media m.each do |e| puts e.caption
And finally, the error message that rspec gives me:
Failures: 1) Experimental counter_cache media_count should == 1 Failure/Error: user.media_count.should == 1 expected: 1 got: 0 (using ==) # ./spec/models/experimental_spec.rb:24:in `block (3 levels) in <top (required)>' Finished in 0.20934 seconds 2 examples, 1 failure
Also note that this happens for the entire counter_cache column in all my models. I also tried several different ways to test this, but they all return the above error message.
Indeed, hoping someone could help me. :)
Thanks for the heaps in advance! Luke