views:

233

answers:

2

I have a MySQL table and I want to pick certain columns to create RSS Feed from it. How to do it using Ruby or Rails or Gems?

+3  A: 

Depending on what I was trying to do, I would probably just go for a simple Ruby script. I would use ActiveRecord so I didn't have to write any SQL. Then I would use either Builder or RubyRSS to generate the feed.

Connecting ActiveRecord to a MySQL server directly is as simple as:

require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "myusername",
:password => "mypassword",
:database  => "mydb"
)

Then you are free to define ActiveRecord models like you would in a regular Rails app.

There are RSS generator examples on the RubyRSS website and a Builder one on the Railscasts website.

hernan43
+1  A: 

Hi there,

Hernan is right...here is are the full steps you'll need to get data from the database (I moved the code to below the steps for easier formatting:

  1. Install ActiveRecord: sudo gem install activerecord
  2. Establish the connection per hernan43's recommendation (preferrably in a separate file, let's call it "connection.rb"
  3. Create a class that uses that connection by inheriting from ActiveRecord
  4. Retrieve record(s) from the table, and use it/them to populate your RSS generator

You don't have to separate everything out into a file...you could put everything below in one file, and remove the 'requires' in files 2 and 3, but it's a convention to separate out your concerns in a manner similar to what I've done.

#1: file connection.rb

require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :database => "appdb",
  :username => "appuser",
  :password => "secret"
)

#2 filename: singural_rss_table_name.rb require 'connection'

class SingularRSSTableName < ActiveRecord::Base
  set_table_name 'real_database_table_name' #if the table name is the lowercase, underscore plural of the class name, then you don't need this line.
end

#3 filename: rss_creator_file.rb

require 'singular_rss_table_name'

# Here you retrieve the rows from your database table, 
# based on the condition that the column 'title' is exactly the text 'article_title'
# there are a lot more options for 
# conditions in ActiveRecord, and you'll probably want to look them up.
records_for_rss_entries = SingularRssTableName.find(:all, :conditions => {:title => 'article_title'})


rss_object = RSS::Maker.new(version) do |feed|
  feed.channel.title = "Example Ruby RSS feed"

  records_for_rss_entries.each do |entry_record|  # here we're iterating through
                                                  # each of the rows we found.
    entry = feed.items.new_item
    entry.title = entry_record.title # at this point, each 'entry_record' is a row in your db table
                                     # use the dot (.) operator to access columns in the table.

    ...
  end  

end

The contents of this answer were partially answered from:

http://rubyrss.com/

and

http://www.agileadvisor.com/2008/01/using-activerecord-outside-rails.html
btelles