Skip to content

Instantly share code, notes, and snippets.

@fnichol
Created March 24, 2011 02:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnichol/884428 to your computer and use it in GitHub Desktop.
Save fnichol/884428 to your computer and use it in GitHub Desktop.
An example using ActiveRecord to connect to a WordPress database
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: rails_proj_development
pool: 5
username: rp_dev
password: wootwoot
host: localhost
wp_development:
adapter: mysql2
encoding: utf8
reconnect: false
database: wpblog_dev
pool: 5
username: wp_user
password: hackable
host: localhost
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: rails_proj_test
pool: 5
username: rp_test
password: wootwoot
host: localhost
wp_test:
adapter: mysql2
encoding: utf8
reconnect: false
database: wpblog_test
pool: 5
username: wp_user
password: hackable
host: localhost
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: rails_proj_production
pool: 5
username: rp_prod
password: wootwoot
host: localhost
wp_production:
adapter: mysql2
encoding: utf8
reconnect: false
database: wpblog_prod
pool: 5
username: wp_user
password: hackable
host: localhost
##
# An example using ActiveRecord to connect to a WordPress database
#
module WordPress
class Post < ActiveRecord::Base
# tell active record to use another database connection
establish_connection "wp_#{Rails.env}"
# set the table name
set_table_name "wp_posts"
# normalize/sanitize some attributes to make things more railsy
alias_attribute "author", "post_author"
alias_attribute "posted_on", "post_date"
alias_attribute "posted_on_gmt", "post_date_gmt"
alias_attribute "content", "post_content"
alias_attribute "title", "post_title"
alias_attribute "excerpt", "post_excerpt"
alias_attribute "status", "post_status"
# add any virtual attributes to abstract gory details
def published?
self.status == "publish"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment