rails - named scoped help

Posted by sameera207 on Stack Overflow See other posts from Stack Overflow or by sameera207
Published on 2010-05-29T07:00:53Z Indexed on 2010/05/29 7:02 UTC
Read the original article Hit count: 457

Filed under:
|
|

Hi All,

I want to write a named scoped to get a record from its id.

Ex: I have a model called Event and its same as doing Event.find(id) (I dont want to use find inside my controller and I want my controller to use a named scoped (for future flexibility))

So I have written a named scoped

named_scope :from_id, lambda { |id| {:conditions => ['id= ?', id] } }

and I'm calling it from my controller like Event.from_id(id)

But my problems is it returns Event object array not only one object

Ex: if I want to get event name I have to write

event = Event.from_id(id)

event[0].name

instead I want to write

event = Event.from_id(id)

event.name

Am I doing something wrong here..

thanks in advance

cheers sameera

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord