<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: activerecord code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 12 Oct 2008 15:13:14 GMT</pubDate>
    <description>DZone Snippets: activerecord code</description>
    <item>
      <title>Use the contents of a WordPress database in your Rails app</title>
      <link>http://snippets.dzone.com/posts/show/1314</link>
      <description>These two models can be used to access the posts and associated comments of a WordPress database.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class WpBlogComment &lt; ActiveRecord::Base&lt;br /&gt;&lt;br /&gt;  # if wordpress tables live in a different database (i.e. 'wordpress') change the following&lt;br /&gt;  # line to set_table_name "wordpress.wp_comments"&lt;br /&gt;  # don't forget to give the db user permissions to access the wordpress db&lt;br /&gt;  set_table_name "wp_comments"&lt;br /&gt;  set_primary_key "comment_ID"&lt;br /&gt;&lt;br /&gt;  belongs_to :post , :class_name =&gt; "WpBlogPost", :foreign_key =&gt; "comment_post_ID"&lt;br /&gt;&lt;br /&gt;  validates_presence_of :comment_post_ID, :comment_author, :comment_content, :comment_author_email&lt;br /&gt;  &lt;br /&gt;  def validate_on_create&lt;br /&gt;    if WpBlogPost.find(comment_post_ID).comment_status != 'open'&lt;br /&gt;      errors.add_to_base('Sorry, comments are closed for this post')&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class WpBlogPost &lt; ActiveRecord::Base&lt;br /&gt;&lt;br /&gt;  set_table_name "wp_posts"&lt;br /&gt;  set_primary_key "ID"&lt;br /&gt;&lt;br /&gt;  has_many :comments, :class_name =&gt; "WpBlogComment", :foreign_key =&gt; "comment_post_ID"&lt;br /&gt;&lt;br /&gt;  def self.find_by_permalink(year, month, day, title)&lt;br /&gt;    find(:first, &lt;br /&gt;         :conditions =&gt; ["YEAR(post_date) = ? AND MONTH(post_date) = ? AND DAYOFMONTH(post_date) = ? AND post_name = ?", year.to_i, month.to_i, day.to_i, title])&lt;br /&gt;  end&lt;br /&gt;end &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 30 Jan 2006 04:10:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1314</guid>
      <author>jazzido (Manuel Aristar&#225;n)</author>
    </item>
  </channel>
</rss>
