Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Manuel Aristarán http://jazzido.freezope.org/jazzido

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Use the contents of a WordPress database in your Rails app

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    # if wordpress tables live in a different database (i.e. 'wordpress') change the following
   5    # line to set_table_name "wordpress.wp_comments"
   6    # don't forget to give the db user permissions to access the wordpress db
   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 


« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS