These two models can be used to access the posts and associated comments of a WordPress database.
class WpBlogComment < ActiveRecord::Base
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