RailsからWordPressの情報を取得する

Ruby On Rails

app/models/wp_post.rb

class WpPost < 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

app/models/wp_comment.rb

class WpComment < 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