Interestingly, the text book Practical Ruby Gems 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 = ''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
On Sep 23, 4:38 am, Thufir <hawat.thu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> > 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 = ''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
On Sep 23, 12:30 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 23, 4:38 am, Thufir <hawat.thu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 ?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
On Oct 13, 6:15 am, Thufir <hawat.thu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 23, 12:30 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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> thanks, > > Thufir