# When and how to use ActiveRecord::Migration

**URL:** <https://www.ruby-forum.com/t/when-and-how-to-use-activerecord-migration/175967>\
**Category:** Rails\
**Created:** [September 23, 2009, 5:40am UTC](https://www.ruby-forum.com/t/when-and-how-to-use-activerecord-migration/175967 "2009-09-23T05:40:21Z")\
**Posts on this page:** 4\
**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 23, 2009, 5:40am UTC](https://www.ruby-forum.com/t/when-and-how-to-use-activerecord-migration/175967/1 "2009-09-23T05:40:21Z")

</div>

Interestingly, the text book

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

uses ActiveRecord::Base wheras ActiveRecord::Migration would give more  
flexibility, right?

How would I implement this change, pls?

thufir@ARRAKIS:~/projects/aggregator$  
thufir@ARRAKIS:~/projects/aggregator$ cat rss2mysql.rb  
require ‘rubygems’  
require ‘active\_record’  
require ‘feed\_tools’  
require ‘yaml’  
require ‘items’

def all

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

Items.connect  
all  
thufir@ARRAKIS:~/projects/aggregator$  
thufir@ARRAKIS:~/projects/aggregator$ cat items.rb

class Items \< ActiveRecord::Base

def Items.connect

```
  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"])

```

# 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

end #connect

end  
thufir@ARRAKIS:~/projects/aggregator$  
thufir@ARRAKIS:~/projects/aggregator$ cat items2.rb  
class Items2 \< ActiveRecord::Migration  
def self.up  
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  
def self.down  
drop\_table :items  
end  
end  
thufir@ARRAKIS:~/projects/aggregator$  
thufir@ARRAKIS:~/projects/aggregator$

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 23, 2009, 9:33am UTC](https://www.ruby-forum.com/t/when-and-how-to-use-activerecord-migration/175967/2 "2009-09-23T09:33:22Z")

</div>

On Sep 23, 4:38 am, Thufir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Interestingly, the text book
> 
> Practical Ruby Gemshttp://www.apress.com/book/view/9781590598115  
> listing 5-1
> 
> uses ActiveRecord::Base wheras ActiveRecord::Migration would give more  
> flexibility, right?

I imagine they are just showing you that there is more than one way to  
do things.

> How would I implement this change, pls?

Which change ?

Fred

---

<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:** [October 13, 2009, 7:17am UTC](https://www.ruby-forum.com/t/when-and-how-to-use-activerecord-migration/175967/3 "2009-10-13T07:17:06Z")

</div>

On Sep 23, 12:30 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I imagine they are just showing you that there is more than one way to  
> do things.
> 
> > How would I implement this change, pls?
> 
> Which change ?

What I mean is, if it’s

class Items \< ActiveRecord::Migration

then there cannot be a query as below:

claims = Items.find( :all)

Or, I suppose you don’t query in that fashion, as the migration file  
will basically just have self.up and down (for me). How is the  
migration in AR linked to the Base class, which is queriable, in a non-  
rails scenario?

I suppose the driver will invoke the migration, but how is the  
migration tied to the model (the Item class, versus Items the  
migration)?

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:** [October 13, 2009, 10:09am UTC](https://www.ruby-forum.com/t/when-and-how-to-use-activerecord-migration/175967/4 "2009-10-13T10:09:11Z")

</div>

On Oct 13, 6:15 am, Thufir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sep 23, 12:30 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:

> > Which change ?
> 
> What I mean is, if it’s
> 
> class Items \< ActiveRecord::Migration
> 
> then there cannot be a query as below:
> 
> claims = Items.find( :all)

Correct.

> Or, I suppose you don’t query in that fashion, as the migration file  
> will basically just have self.up and down (for me). How is the  
> migration in AR linked to the Base class, which is queriable, in a non-  
> rails scenario?

I don’t think it is.

> I suppose the driver will invoke the migration, but how is the  
> migration tied to the model (the Item class, versus Items the  
> migration)?

There is no correspondence between models and migrations. For a given  
model there will frequently be a migration that creates the  
appropriate table but you don’t have to go that way.

Fred
