Please help me out

20 views
Skip to first unread message

lekha p.

unread,
Dec 23, 2014, 9:50:16 AM12/23/14
to rubyonra...@googlegroups.com
Hi,

I have a User Model which includes attributes Street, City. I want to
get array of Streets of users, when street is blank or nil i want to
pick up city of user. By searching i got to know i can get array of
streets using
User.pluck(:street), what is the query to find city of user when street
is nil.

Thanks in advance.

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Dec 23, 2014, 10:02:40 AM12/23/14
to rubyonra...@googlegroups.com
On 23 December 2014 at 14:49, lekha p. <li...@ruby-forum.com> wrote:
> Hi,
>
> I have a User Model which includes attributes Street, City. I want to
> get array of Streets of users, when street is blank or nil i want to
> pick up city of user. By searching i got to know i can get array of
> streets using
> User.pluck(:street), what is the query to find city of user when street
> is nil.

Do you mean all the cities where the street is nil? If so then you
are going about it the wrong way. First you want to find all records
where the street is nil, so something like
User.where( "street is null" )
That assumes you actually mean null entry rather than a blank string.
If you also mean a blank string then you can modify the where clause
appropriately.
Then you can extract the cities if you need to.

I suspect that you may be beginner with Rails, in which case I
strongly urge you to work right through a good tutorial such as
railstutorial.org (which is free to use online). That will show you
the basics of rails, so avoiding the need to ask for help with basic
questions.

Colin

Almas Sapargali

unread,
Dec 24, 2014, 3:51:19 AM12/24/14
to rubyonra...@googlegroups.com
If you like ruby queries like me then you can use
User.where(street: nil)
Reply all
Reply to author
Forward
0 new messages