These two models can be used to access the posts and associated comments of a WordPress database.
1
2 class WpBlogComment < ActiveRecord::Base
3
4
5
6
7 set_table_name "wp_comments"
8 set_primary_key "comment_ID"
9
10 belongs_to :post , :class_name => "WpBlogPost", :foreign_key => "comment_post_ID"
11
12 validates_presence_of :comment_post_ID, :comment_author, :comment_content, :comment_author_email
13
14 def validate_on_create
15 if WpBlogPost.find(comment_post_ID).comment_status != 'open'
16 errors.add_to_base('Sorry, comments are closed for this post')
17 end
18 end
19
20 end
1
2 class WpBlogPost < ActiveRecord::Base
3
4 set_table_name "wp_posts"
5 set_primary_key "ID"
6
7 has_many :comments, :class_name => "WpBlogComment", :foreign_key => "comment_post_ID"
8
9 def self.find_by_permalink(year, month, day, title)
10 find(:first,
11 :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])
12 end
13 end