Using script/console

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

Hi,

Youre Item class should read:
belongs_to :feed #singular

Then in console:

f = Feed.find :first
f.items.create :title => ‘test_item’

Thats it

On Mon, 10 Dec 2007 14:23:32 +0100, Peter De jong wrote:

Youre Item class should read:
belongs_to :feed #singular

Thank you :slight_smile:

I’ve been mucking about with svn, and completely started over this
project with a (mildly) different schema. This is my first foray into
the console, pardon, I haven’t researched much as I’ve just been trying
to get back to this point:

thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ script/console
Loading development environment.

f = Feed.find :first
=> #<Feed:0xb7000188 @attributes={“item_id”=>“0”, “feed”=>“feed1”,
“id”=>“1”}>

f.items.create :item => ‘test_item’
=> #<Item:0xb6ff4680 @new_record=false,
@errors=#<ActiveRecord::Errors:0xb6ff21dc @errors={},
@base=#<Item:0xb6ff4680 …>>, @attributes={“id”=>3, “feed_id”=>1,
“item”=>“test_item”}>

?> f.items.create :feed => ‘test_item’
ActiveRecord::AssociationTypeMismatch: Feed expected, got String
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/associations/association_proxy.rb:148:in
raise_on_type_mismatch' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations/belongs_to_association.rb:22:inreplace’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/associations.rb:923:in feed=' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1675:insend’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:1675:in attributes=' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1674:ineach’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:1674:in attributes=' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1508:ininitialize_without_callbacks’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/callbacks.rb:225:in initialize' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations/has_many_association.rb:13:innew’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/associations/has_many_association.rb:13:in build' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations/association_collection.rb:93:increate’
from (irb):4

quit
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ script/console
Loading development environment.

f=feed.find :first
NameError: undefined local variable or method `feed’ for
#Object:0xb7d5d9ac
from (irb):1

f=Feed.find :first
=> #<Feed:0xb70b1b54 @attributes={“item_id”=>“0”, “feed”=>“feed1”,
“id”=>“1”}>

f.items.create :feed => ‘test_item’
ActiveRecord::AssociationTypeMismatch: Feed expected, got String
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/associations/association_proxy.rb:148:in
raise_on_type_mismatch' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations/belongs_to_association.rb:22:inreplace’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/associations.rb:923:in feed=' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1675:insend’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:1675:in attributes=' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1674:ineach’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:1674:in attributes=' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1508:ininitialize_without_callbacks’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/callbacks.rb:225:in initialize' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations/has_many_association.rb:13:innew’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/associations/has_many_association.rb:13:in build' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/associations/association_collection.rb:93:increate’
from (irb):3

.quit
SyntaxError: compile error
(irb):4: syntax error, unexpected ‘.’
.quit
^
from (irb):4

quit
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat db/migrate/001_categories.rb
class Categories < ActiveRecord::Migration
def self.up
create_table :categories do |table|
table.column :feed_id, :integer
table.column :category, :string
end
end

    def self.down
            drop_table :categories
    end

end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat db/migrate/002_feeds.rb
class Feeds < ActiveRecord::Migration
def self.up
create_table :feeds do |table|
table.column :item_id, :integer
table.column :feed, :string
end
end

    def self.down
            drop_table :feeds
    end

end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat db/migrate/003
cat: db/migrate/003: No such file or directory
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 :feed_id, :integer
table.column :item, :string
end
end

    def self.down
            drop_table :items
    end

end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat app/models/category.rb
class Category < ActiveRecord::Base
has_many :feeds
end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat app/models/feed.rb
class Feed < ActiveRecord::Base
belongs_to :category
has_many :items
end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ cat app/models/item.rb
class Item < ActiveRecord::Base
belongs_to :feed
end
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $

thanks,

Thufir

I figured out some syntax for the console! thanks to <> :slight_smile:

In the following I do some of the CRUD operations. However, as soon as
I
gave myself a pat on the back I reviewed the models at <http://
strawr.googlecode.com/svn/trunk/app/models/> and it made me think (oh,
the horror!).

It should be that a single Category object/row may have multiple Feed
objects/rows associated with it – that’s what I want. I suppose it’s
also possible for a Feed object to belong to multiple Category objects/
rows? That would be preferable.

Maybe I have a models problems? In any event, how would I update one of
the Category rows/objects so that there multiple Feed objects/rows
associated with it?

thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $ script/console
Loading development environment.

Category.find :all
=> [#<Category:0xb704c934 @attributes={“category”=>“cat_one”, “id”=>“1”,
“feed_id”=>“0”}>, #<Category:0xb704c8f8 @attributes=
{“category”=>“cat_two”, “id”=>“2”, “feed_id”=>“0”}>,
#<Category:0xb704c8d0 @attributes={“category”=>“cat_three”, “id”=>“3”,
“feed_id”=>“0”}>]

?> Feed.find :all
=> [#<Feed:0xb703e2d0 @attributes={“item_id”=>“0”, “feed”=>“feed_one”,
“id”=>“1”}>, #<Feed:0xb703e294 @attributes={“item_id”=>“0”,
“feed”=>“feed_two”, “id”=>“2”}>, #<Feed:0xb703e26c @attributes=
{“item_id”=>“0”, “feed”=>“feed_three”, “id”=>“3”}>]

?> c=Category.new
=> #<Category:0xb703cb10 @new_record=true, @attributes=
{“category”=>“NULL”, “feed_id”=>0}>

?> c
=> #<Category:0xb703cb10 @new_record=true, @attributes=
{“category”=>“NULL”, “feed_id”=>0}>

?> c.update_attribute :category, “cat_four_has_feeds”
=> true

?> Category.find :all
=> [#<Category:0xb7031d3c @attributes={“category”=>“cat_one”, “id”=>“1”,
“feed_id”=>“0”}>, #<Category:0xb7031d14 @attributes=
{“category”=>“cat_two”, “id”=>“2”, “feed_id”=>“0”}>,
#<Category:0xb7031cec @attributes={“category”=>“cat_three”, “id”=>“3”,
“feed_id”=>“0”}>, #<Category:0xb7031cc4 @attributes=
{“category”=>“cat_four_has_feeds”, “id”=>“4”, “feed_id”=>“0”}>]

?>
?> c.update_attribute :feed_id, 1
=> true

?> Category.find :all
=> [#<Category:0xb702a62c @attributes={“category”=>“cat_one”, “id”=>“1”,
“feed_id”=>“0”}>, #<Category:0xb702a604 @attributes=
{“category”=>“cat_two”, “id”=>“2”, “feed_id”=>“0”}>,
#<Category:0xb702a5dc @attributes={“category”=>“cat_three”, “id”=>“3”,
“feed_id”=>“0”}>, #<Category:0xb702a5b4 @attributes=
{“category”=>“cat_four_has_feeds”, “id”=>“4”, “feed_id”=>“1”}>]

?>
?> Feed.find :all
=> [#<Feed:0xb7027134 @attributes={“item_id”=>“0”, “feed”=>“feed_one”,
“id”=>“1”}>, #<Feed:0xb702710c @attributes={“item_id”=>“0”,
“feed”=>“feed_two”, “id”=>“2”}>, #<Feed:0xb70270e4 @attributes=
{“item_id”=>“0”, “feed”=>“feed_three”, “id”=>“3”}>]

?> quit
thufir@arrakis ~/Desktop/strawr $
thufir@arrakis ~/Desktop/strawr $

thanks,

Thufir

On 10 Dec 2007, at 15:23, Thufir wrote:

project with a (mildly) different schema. This is my first foray into
the console, pardon, I haven’t researched much as I’ve just been
trying
to get back to this point:

The console isn’t magic. Broadly speaking it’s just like cracking open
your application and fiddling with it’s innards: what works in some
model code should work in the console

?> f.items.create :feed => ‘test_item’
ActiveRecord::AssociationTypeMismatch: Feed expected, got String

Exactly what it says on the tin: if item is an item, item.feed should
be an instance of feed, not a String

Fred

On Tue, 11 Dec 2007 20:53:49 -0800, fredistic wrote:

Once you’re familiar with getting things done in the console, put it in
a ruby file such as consoletest.rb and from the console do: load
“consoletest.rb”.

Thanks :slight_smile:

-Thufir

Take a look at this console tutorial:
http://clarkware.com/cgi/blosxom/2006/04/04

Once you’re familiar with getting things done in the console, put it
in a ruby file such as consoletest.rb and from the console do: load
“consoletest.rb”.

fredistic