Having a tough time creating a where clause and/or query for the
following situation for a table. Will appreciate some help:

1. Need to filter records for a specific client_id. No problem,
straightforward where condition

2. For the specific client records found in step 1, need to further
filter records where the status is "published" for if the record was not
created by the current user, OR, regardless of the
"published/unpublished" status value if the record was created by the
current user. Note that the current user is identified by the
"current_user" method and there is a field in the table which is called
"created_by" that stores the id of the user who created this record.

What is the best way of representing such query?

Thanks.

Bharat

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Jim Ruther Nill at Dec 26, 2011 at 2:13 am

    On Mon, Dec 26, 2011 at 6:37 AM, Bharat Ruparel wrote:

    Having a tough time creating a where clause and/or query for the
    following situation for a table. Will appreciate some help:

    1. Need to filter records for a specific client_id. No problem,
    straightforward where condition

    2. For the specific client records found in step 1, need to further
    filter records where the status is "published" for if the record was not
    created by the current user, OR, regardless of the
    "published/unpublished" status value if the record was created by the
    current user. Note that the current user is identified by the
    "current_user" method and there is a field in the table which is called
    "created_by" that stores the id of the user who created this record.
    Foo.where(:client_id => client_id).where('(status = ? AND created_by != ?)
    OR (created_by = ?)', 'published', current_user.id, current_user.id)

    What is the best way of representing such query?

    Thanks.

    Bharat

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

    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.

    --
    -------------------------------------------------------------
    visit my blog at http://jimlabs.heroku.com

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 25, '11 at 10:38p
activeDec 26, '11 at 2:13a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Bharat Ruparel: 1 post Jim Ruther Nill: 1 post

People

Translate

site design / logo © 2021 Grokbase