# Using script/console

**URL:** <https://www.ruby-forum.com/t/using-script-console/122636>\
**Category:** Rails\
**Created:** [December 10, 2007, 10:31am UTC](https://www.ruby-forum.com/t/using-script-console/122636 "2007-12-10T10:31:19Z")\
**Posts on this page:** 7\
**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:** [December 10, 2007, 10:31am UTC](https://www.ruby-forum.com/t/using-script-console/122636/1 "2007-12-10T10:31:19Z")

</div>

For the project at [http://code.google.com/p/strawr/](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](http://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](http://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

---

<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:** [December 10, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/using-script-console/122636/2 "2007-12-10T14:23:32Z")

</div>

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

---

<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:** [December 10, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/using-script-console/122636/3 "2007-12-10T16:24:15Z")

</div>

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

> Youre Item class should read:  
> belongs\_to :feed #singular

Thank you 🙂

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:in`replace’  
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:in`send’  
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:in`each’  
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:in`initialize\_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:in`new’  
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:in`create’  
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:in`replace’  
> > 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:in`send’  
> > 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:in`each’  
> > 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:in`initialize\_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:in`new’  
> > 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:in`create’  
> > 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

---

<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:** [December 11, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/using-script-console/122636/4 "2007-12-11T16:00:23Z")

</div>

I figured out some syntax for the console! thanks to \<\> 🙂

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/](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

---

<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:** [December 10, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/using-script-console/122636/5 "2007-12-10T16:34:15Z")

</div>

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

---

<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:** [December 12, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/using-script-console/122636/6 "2007-12-12T21:07:54Z")

</div>

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 🙂

-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:** [December 12, 2007, 8:59am UTC](https://www.ruby-forum.com/t/using-script-console/122636/7 "2007-12-12T08:59:55Z")

</div>

Take a look at this console tutorial:  
[http://clarkware.com/cgi/blosxom/2006/04/04](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
