Console syntax

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

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

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

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
[email protected]

Excellent, thanks all :slight_smile:

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