# Decompose ActiveRecord script into methods

**URL:** <https://www.ruby-forum.com/t/decompose-activerecord-script-into-methods/175907>\
**Category:** Ruby\
**Created:** [September 22, 2009, 5:46pm UTC](https://www.ruby-forum.com/t/decompose-activerecord-script-into-methods/175907 "2009-09-22T17:46:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [September 22, 2009, 5:46pm UTC](https://www.ruby-forum.com/t/decompose-activerecord-script-into-methods/175907/1 "2009-09-22T17:46:28Z")

</div>

If I commend out the def and end lines for the “all” method, the script  
runs fine. However, the goal is to decompose the script into several  
methods, which fails with:

thufir@ARRAKIS:~/projects/aggregator$  
thufir@ARRAKIS:~/projects/aggregator$ ruby rss2mysql.rb  
rss2mysql.rb:19: class definition in method body  
thufir@ARRAKIS:~/projects/aggregator$

The script is adapted from:

Practical Ruby Gems  
[http://www.apress.com/book/view/9781590598115](http://www.apress.com/book/view/9781590598115)  
Chapter 5

require ‘rubygems’  
require ‘active\_record’  
require ‘feed\_tools’  
require ‘yaml’

def all

```
  db = YAML.load_file("database.yml")
ActiveRecord::Base.establish_connection(
    :adapter => db["adapter"],
    :host => db["host"],
    :username => db["username"],
    :password => db["password"],
    :database => db["database"])

```

class Items \< ActiveRecord::Base  
end

# If the table doesn’t exist, we’ll create it.

unless Items.table\_exists?  
ActiveRecord::Schema.define do  
create\_table :items do |t|  
t.column :title, :string  
t.column :content, :string  
t.column :source, :string  
t.column :url, :string  
t.column :timestamp, :timestamp  
t.column :keyword\_id, :integer  
t.column :guid, :string  
t.column :html, :string  
end  
end  
end

feed\_url = ‘[Slashdot](http://www.slashdot.org/index.rss)’

feed=FeedTools::Feed.open(feed\_url)  
feed.items.each do |feed\_item|  
unless (Items.find\_by\_title(feed\_item.title)   
or Items.find\_by\_url(feed\_item.link)   
or Items.find\_by\_guid(feed\_item.guid))  
puts “processing item ‘#{feed\_item.title}’ - new”

```
Items.new do |newitem|
   newitem.title=feed_item.title.gsub(/<[^>]*>/, '')
   newitem.guid=feed_item.guid
   if feed_item.publisher.name
       newitem.source=feed_item.publisher.name
   end
   newitem.url=feed_item.link
   newitem.content=feed_item.description
   newitem.timestamp=feed_item.published
   newitem.save
end

```

else  
puts “processing item ‘#{feed\_item.title}’ - old”  
end  
end

end #end all

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [September 22, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/decompose-activerecord-script-into-methods/175907/2 "2009-09-22T17:55:16Z")

</div>

On Tue, Sep 22, 2009 at 4:45 PM, Thufir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I commend out the def and end lines for the “all” method, the script  
> runs fine. However, the goal is to decompose the script into several  
> methods, which fails with:
> 
> thufir@ARRAKIS:~/projects/aggregator$  
> thufir@ARRAKIS:~/projects/aggregator$ ruby rss2mysql.rb  
> rss2mysql.rb:19: class definition in method body  
> thufir@ARRAKIS:~/projects/aggregator$

The error message tells you exactly what is wrong:

> class Items \< ActiveRecord::Base  
> end

You can’t have a class declaration like this inside a method body.

Instead of making a single method around the whole script, make at  
least 2 methods, one which covers the code before the class  
declaration, then one which covers the code after it. Or move the  
class definition to the top of the file, before def all.

> thanks,
> 
> Thufir

–  
Paul S.  
[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [September 23, 2009, 3:23am UTC](https://www.ruby-forum.com/t/decompose-activerecord-script-into-methods/175907/3 "2009-09-23T03:23:18Z")

</div>

On Wed, 23 Sep 2009 00:54:23 +0900, Paul S. wrote:

> top of the file, before def all.  
> Ah, before the class, that makes sense. I suppose what I was wanting  
> was  
> to declare the class in a separate file and then “require ‘Items.rb’”,  
> how would that work?

thanks,

Thufir
