Created
March 24, 2011 02:12
-
-
Save fnichol/884428 to your computer and use it in GitHub Desktop.
An example using ActiveRecord to connect to a WordPress database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# An example using ActiveRecord to connect to a WordPress database | |
# | |
module WordPress | |
class Post < ActiveRecord::Base | |
# tell active record to use another database connection | |
# skip the Rails.env part if you only have one wordpress instance | |
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