# Console syntax

**URL:** <https://www.ruby-forum.com/t/console-syntax/128464>\
**Category:** Rails\
**Created:** [February 7, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/console-syntax/128464 "2008-02-07T22:07:15Z")\
**Posts on this page:** 5\
**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:** [February 7, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/console-syntax/128464/1 "2008-02-07T22:07:15Z")

</div>

What’s the syntax for something like:

f=Foo.(id==1)

Here’s what I’ve tried:

thufir@arrakis ~/goodfellow-tool $  
thufir@arrakis ~/goodfellow-tool $ script/console  
Loading development environment.

> > Call.find :all  
> > =\> [#\<Call:0xb6fca790 @attributes={“name”=\>“zero”, “id”=\>“1”,  
> > “end”=\>“2007-01-01 18:00:00”, “start”=\>“2007-01-01 17:00:00”,  
> > “login”=\>“0123”}\>]
> 
> >

?\> c =\> Call.1  
SyntaxError: compile error  
(irb):3: syntax error, unexpected tASSOC, expecting $end  
c =\> Call.1  
^  
from (irb):3

> >

?\> c = Call.1  
SyntaxError: compile error  
(irb):5: no . floating literal anymore; put 0 before dot  
c = Call.1  
^  
(irb):5: syntax error, unexpected tINTEGER  
from (irb):5

> > quit  
> > thufir@arrakis ~/goodfellow-tool $

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:** [February 7, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/console-syntax/128464/2 "2008-02-07T22:21:24Z")

</div>

On 7 Feb 2008, at 21:06, Thufir wrote:

> What’s the syntax for something like:
> 
> f=Foo.(id==1)

Haven’t you already had to do this dozens of times in your rails app?  
It’s just f = Foo.find 1

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:** [February 8, 2008, 1:21am UTC](https://www.ruby-forum.com/t/console-syntax/128464/3 "2008-02-08T01:21:07Z")

</div>

On Thu, 07 Feb 2008 21:16:49 +0000, Frederick C. wrote:

> Haven’t you already had to do this dozens of times in your rails app?  
> It’s just f = Foo.find 1

I’m trying to use the console a bit more. Does that specify that the id  
field==1, or does that just do a general search through all fields? I’d  
like to specify the id field/attribute.

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:** [February 8, 2008, 1:33am UTC](https://www.ruby-forum.com/t/console-syntax/128464/4 "2008-02-08T01:33:30Z")

</div>

On Feb 7, 2008, at 7:17 PM, Thufir wrote:

> thanks,
> 
> Thufir

The console it just an irb session that has been initialized with your  
rails application. Anything that you can do with a model, you can do  
in the console. Not so much with controllers though as they expect to  
have request object. (In fact, this is IMHO one of the strongest  
examples of why the “fat model/skinny controller” style is the way to  
go.)

Are you asking about how to indicate that your primary key is not  
called ‘id’? If so, look at the docs for ‘set\_primary\_key’.

If you want to match some other column, you have the dynamic finders:  
Foo.find\_by\_id(1)  
or explicitly using conditions:  
Foo.find(:first, :conditions =\> [‘id = ?’, 1])  
Foo.find(:first, :conditions =\> { :id =\> 1 })

In every case, the docs are your friend here.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<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:** [February 9, 2008, 5:35am UTC](https://www.ruby-forum.com/t/console-syntax/128464/5 "2008-02-09T05:35:48Z")

</div>

Excellent, thanks all 🙂

thufir@arrakis ~/goodfellow-tool $  
thufir@arrakis ~/goodfellow-tool $ script/console  
Loading development environment.

> > Call.find :all  
> > =\> [#\<Call:0xb7087750 @attributes={“id”=\>“1”, “comment”=\>“start work”,  
> > “login\_id”=\>“1”, “created\_at”=\>“2008-02-08 15:12:13”}\>, #\<Call:  
> > 0xb7087714 @attributes={“id”=\>“2”, “comment”=\>“start call”,  
> > “login\_id”=\>“1”, “created\_at”=\>“2008-02-08 15:12:13”}\>, #\<Call:  
> > 0xb70876ec @attributes={“id”=\>“3”, “comment”=\>“start break”,  
> > “login\_id”=\>“1”, “created\_at”=\>“2008-02-08 15:12:13”}\>, #\<Call:  
> > 0xb70876c4 @attributes={“id”=\>“4”, “comment”=\>“start work”,  
> > “login\_id”=\>“2”, “created\_at”=\>“2008-02-08 15:12:13”}\>]
> 
> > Call.find\_by\_login\_id(1)  
> > =\> #\<Call:0xb7081e40 @attributes={“id”=\>“1”, “comment”=\>“start work”,  
> > “login\_id”=\>“1”, “created\_at”=\>“2008-02-08 15:12:13”}\>
> 
> > Call.find(:all, :conditions =\> { :login\_id =\> 1 })  
> > =\> [#\<Call:0xb707c328 @attributes={“id”=\>“1”, “comment”=\>“start work”,  
> > “login\_id”=\>“1”, “created\_at”=\>“2008-02-08 15:12:13”}\>, #\<Call:  
> > 0xb707c300 @attributes={“id”=\>“2”, “comment”=\>“start call”,  
> > “login\_id”=\>“1”, “created\_at”=\>“2008-02-08 15:12:13”}\>, #\<Call:  
> > 0xb707c2d8 @attributes={“id”=\>“3”, “comment”=\>“start break”,  
> > “login\_id”=\>“1”, “created\_at”=\>“2008-02-08 15:12:13”}\>]
> 
> > quit  
> > thufir@arrakis ~/goodfellow-tool $

-Thufir
