One cool thing about wordpress is that you can get a feed of a specific category by going to /category/category-name/feed or a specific tag by going to /tag/tag-name/feed
require 'open-uri'
require 'hpricot'
doc = Hpricot.parse(open("http://myawesomeblog.com/category/welcome/feed"))
(doc/:item).each do |xml_product|
puts xml_product.search("/title").first.children.first.raw_string
puts xml_product.search("/pubDate").first.children.first.raw_string
end
Stolen from: 10 Awesome Ruby on Rails Techniques to Get You Started
Group operations in a transaction: ActiveRecord wraps the creation or update of a record in a single transaction. Multiple inserts will then generate many transactions (one for each insert). Grouping multiple inserts in one single transaction will speed things up.
Insead of:
diners_in_nj.each do |d|
Diner.create({:disco_fries => d.disco_fries})
end
Use:
Diner.transaction do
diners_in_nj.each do |d|
Diner.create({:disco_fries => d.disco_fries})
end
end
or for rolling back the whole transaction if any insert fails, use:
Diner.transaction do
diners_in_nj.each do |d|
diner = Diner.new({:disco_fries => d.disco_fries})
diner.save!
end
end
A less annoying database.yml file when you use the same database for all three environments
defaults: &defaults
adapter: mysql
encoding: utf8
reconnect: false
pool: 5
username: admin
password: welcome1
development:
database: friendster_dev
<<: *defaults
test:
database: friendster_test
<<: *defaults
production:
database: friendster
<<: *defaults
How come its always so hard to find documentation for the database.yml file?
/test/fixtures/users.yml
<%
def auto_increment
@id ||= 0; @id += 1
end
%>
tommy:
id: <%= auto_increment %>
email: tommy@thedocks.com
created_at: <%= 6.months.ago.to_s(:db) %>
gina:
id: <%= auto_increment %>
email: gina@jerseydiner.com
created_at: <%= 1.minute.ago.to_s(:db) %>
/test/unit/user_test.rb
fixtures :users
def test_can_access_tommy_fixture
assert(users(:tommy) != nil)
end
Import your fixture records into the current environment: rake db:fixtures:load
Sometimes when things get crazy, you need to reference the default stuff.
class ArticlesController < ApplicationController
# GET /articles
# GET /articles.xml
def index
@articles = Article.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @articles }
end
end
# GET /articles/1
# GET /articles/1.xml
def show
@article = Article.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @article }
format.json { render :json => @article }
end
end
# GET /articles/new
# GET /articles/new.xml
def new
@article = Article.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @article }
end
end
# GET /articles/1/edit
def edit
@article = Article.find(params[:id])
end
# POST /articles
# POST /articles.xml
def create
@article = Article.new(params[:article])
respond_to do |format|
if @article.save
flash[:notice] = 'Article was successfully created.'
format.html { redirect_to(@article) }
format.xml { render :xml => @article, :status => :created, :location => @article }
else
format.html { render :action => "new" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
# PUT /articles/1
# PUT /articles/1.xml
def update
@article = Article.find(params[:id])
respond_to do |format|
if @article.update_attributes(params[:article])
flash[:notice] = 'Article was successfully updated.'
format.html { redirect_to(@article) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.xml
def destroy
@article = Article.find(params[:id])
@article.destroy
respond_to do |format|
format.html { redirect_to(articles_url) }
format.xml { head :ok }
end
end
end