Use The Contents Of A WordPress Database In Your Rails App
Join the DZone community and get the full member experience.Join For Free
These two models can be used to access the posts and associated comments of a WordPress database.
class WpBlogComment < ActiveRecord::Base # if wordpress tables live in a different database (i.e. 'wordpress') change the following # line to set_table_name "wordpress.wp_comments" # don't forget to give the db user permissions to access the wordpress db set_table_name "wp_comments" set_primary_key "comment_ID" belongs_to :post , :class_name => "WpBlogPost", :foreign_key => "comment_post_ID" validates_presence_of :comment_post_ID, :comment_author, :comment_content, :comment_author_email def validate_on_create if WpBlogPost.find(comment_post_ID).comment_status != 'open' errors.add_to_base('Sorry, comments are closed for this post') end end end
class WpBlogPost < ActiveRecord::Base set_table_name "wp_posts" set_primary_key "ID" has_many :comments, :class_name => "WpBlogComment", :foreign_key => "comment_post_ID" def self.find_by_permalink(year, month, day, title) find(:first, :conditions => ["YEAR(post_date) = ? AND MONTH(post_date) = ? AND DAYOFMONTH(post_date) = ? AND post_name = ?", year.to_i, month.to_i, day.to_i, title]) end end
Opinions expressed by DZone contributors are their own.