For the project at http://code.google.com/p/strawr/, I want to use the
console to run some queries. I can query a specific feed object, I
also saw how to do a search, but I want the equivalent to “SELECT * FROM
feeds”, but using the console.
Also, as there’s a relationship between feeds and items, I’d like to
create link a feeds row (this is a Feed object?) to an items row, but I
can’t find the syntax. Any tips please?
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ script/console
Loading development environment.
f=Feed.find(1)
=> #<Feed:0xb70bf6dc @attributes={“item_id”=>“0”, “title”=>“aFeed”,
“id”=>“1”, “category_id”=>“0”, “location”=>“www.somewhere.com”}>
?> i=Item.find(1)
=> #<Item:0xb70b2144 @attributes={“title”=>“item title”, “is_read”=>“1”,
“id”=>“1”, “pub_date”=>“2007/12/10”, “description”=>“an item
description”, “feed_id”=>“0”, “link”=>“www.link.com”}>
?> quit
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat db/migrate/001_feeds.rb
class Feeds < ActiveRecord::Migration
def self.up
create_table :feeds do |table|
table.column :title, :string
table.column :location, :string
table.column :category_id, :integer
table.column :item_id, :integer
end
end
def self.down
drop_table :feeds
end
end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat db/migrate/003_items.rb
class Items < ActiveRecord::Migration
def self.up
create_table :items do |table|
table.column :title, :string
table.column :is_read, :integer
table.column :link, :string
table.column :pub_date, :string
table.column :description, :string
table.column :feed_id, :integer
end
end
def self.down
drop_table :items
end
end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat app/models/feed.rb
class Feed < ActiveRecord::Base
has_many :items
end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat app/models/item.rb
class Item < ActiveRecord::Base
belongs_to :feeds
end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cd db/
thufir@arrakis ~/Desktop/strawr/db $
thufir@arrakis ~/Desktop/strawr/db $ sqlite3 development.sqlite3
SQLite version 3.4.1
Enter “.help” for instructions
sqlite>
sqlite> .schema
CREATE TABLE categories (“id” INTEGER PRIMARY KEY NOT NULL, “parent_num”
integer DEFAULT NULL, “title” varchar(255) DEFAULT NULL, “feed_id”
integer DEFAULT NULL);
CREATE TABLE feeds (“id” INTEGER PRIMARY KEY NOT NULL, “title” varchar
(255) DEFAULT NULL, “location” varchar(255) DEFAULT NULL, “category_id”
integer DEFAULT NULL, “item_id” integer DEFAULT NULL);
CREATE TABLE items (“id” INTEGER PRIMARY KEY NOT NULL, “title” varchar
(255) DEFAULT NULL, “is_read” integer DEFAULT NULL, “link” varchar(255)
DEFAULT NULL, “pub_date” varchar(255) DEFAULT NULL, “description”
varchar
(255) DEFAULT NULL, “feed_id” integer DEFAULT NULL);
CREATE TABLE nodes (“id” INTEGER PRIMARY KEY NOT NULL, “obj_num” integer
DEFAULT NULL, “norder” varchar(255) DEFAULT NULL, “type” varchar(255)
DEFAULT NULL);
CREATE TABLE posts (“id” INTEGER PRIMARY KEY NOT NULL, “post” varchar
(255) DEFAULT NULL);
CREATE TABLE schema_info (version integer);
sqlite>
sqlite> .quit
thufir@arrakis ~/Desktop/strawr/db $
thufir@arrakis ~/Desktop/strawr/db $
thanks,
Thufir