0
目標:找到或創建一個類別,將其添加到連接表通過飼料Rails的增加從模型連接表
修訂在解析時:
has_many :category_feeds
has_many :categories, :through => :category_feeds
def self.nokogiri_get_feed(url)
doc = Nokogiri::HTML(open(url))
doc.search('feed entry').each do |item|
unless exists? :guid => item.css('id').text
categories = []
item.xpath('content').each do |i|
i.css('subject').each do |subject|
name = subject['subjectname']
categories << Category.find_or_create_by_name(name)
end
end
create!(:name => item.css('title').text, :url => item.at_css("link")[:href], :guid => item['id'], :categories => categories)
end
end
end
看着指南,但你的輸入和http://railspikes.com/2008/8/11/understanding-map-and-reduce之間的工作。用可能的答案更新了問題,但是這是正確的嗎?必須添加類別= []並在主題後移動創建。 – pcasa 2011-05-07 11:57:43
只有當你錯誤地表達了你的目標,並且實際上你想用新的**替換任何現有的類別,而不是**按照你的要求添加**。 – smathy 2011-05-07 20:07:17