Je ne vois pas ce qui ne va pas avec ce code. Voici les spécifications:Myopia WRT has_many: à travers
it "Meta tags should not duplicate for a particular page" do
p1 = Page.create!(:title => 'first page', :body_text => 'p1 body text')
p2 = Page.create!(:title => 'second page', :body_text => 'p2 body text')
p1.add_meta_tag(:content => 'alexa', :description => '100')
p2.add_meta_tag(:content => 'alexa', :description => '200')
p1.meta_tags.where("content = 'alexa'").should have(1).record
p2.meta_tags.where("content = 'alexa'").should have(1).record
end
Les modèles sont les suivants:
class Page < AR::Base
has_many :page_to_meta_tag_maps
has_many :meta_tags, :through => :page_to_meta_tag_maps
# BONEHEAD PROBLEM PROBABLY HERE...
def add_meta_tag(options)
@cm = self.meta_tags.where(["content=?", options[:content]]).first
@current_meta = @cm || MetaTag.create(options)
@current_meta.description = options[:description]
self.meta_tags << @current_meta
end
end
class PageToMetaTagMap < ActiveRecord::Base
belongs_to :page
belongs_to :meta_tag
end
class MetaTag < ActiveRecord::Base
attr_accessible :content, :description
has_many :page_to_meta_tag_maps
has_many :pages, :through => :page_to_meta_tag_maps
end
Il est à défaut la spécification disant il y a 2 enregistrements, et non 1. Les journaux montrent que Rails est en train de faire un INSERT chaque fois, et @ cm dans la zone BONEHEAD PROBLEM est toujours nul. Je dois fondamentalement mal comprendre quelque chose ici.
Je l'aime quand cela se produit et il y a la pression du temps :(