# Post.where() causing 500 error

**URL:** https://meta.discourse.org/t/post-where-causing-500-error/40896
**Category:** Development
**Created:** [March 11, 2016, 10:33am UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896 "2016-03-11T10:33:23Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Andrew\_Byrne](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/andrew_byrne/32/116413_2.png) [@Andrew\_Byrne](https://meta.discourse.org/u/Andrew_Byrne)
#### Post date: [March 11, 2016, 10:33am UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896/1 "2016-03-11T10:33:23Z")

</div>

Noooob question (2 days ruby experience 😉  
I have a plugin almost doing what I want but have a hang up here:

```
 posts = Post.where('user_id = ? and post_number = ?', params["uid"], params["tid"])
		      .order(created_at: :desc)
		      .limit(params["total"])

```

I’ve tried a number of versions. but they all case errors .  
It feels more like a query issues than syntax but I bow before the experts edu-mecate me.

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [March 11, 2016, 10:40am UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896/2 "2016-03-11T10:40:03Z")

</div>

What does the logs say? 🙂

---

<div class="post-metadata">

### Author: ![Andrew\_Byrne](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/andrew_byrne/32/116413_2.png) [@Andrew\_Byrne](https://meta.discourse.org/u/Andrew_Byrne)
#### Post date: [March 11, 2016, 11:23am UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896/3 "2016-03-11T11:23:06Z")

</div>

so good of you to ask, and admittedly that helped… 😃

BUT perhaps you can assist further with my query! The issue was the input value  
while I can do

ok  
` Post.where('user_id: '1,2') #works`

```
Post.where('topic_id: '4,5') #works

```

Fails  
`Post.where('user_id = ? or topic_id = ?', '1,2', '4, 5') #fails!`

So again I assume! I’ll need to iterate over each user id, but… NOOOOB (assistance greatly appreciated!

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [March 11, 2016, 11:41am UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896/4 "2016-03-11T11:41:25Z")

</div>

So why did it fail? What is the error that was raised when you ran the code? 🙂

---

<div class="post-metadata">

### Author: ![Andrew\_Byrne](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/andrew_byrne/32/116413_2.png) [@Andrew\_Byrne](https://meta.discourse.org/u/Andrew_Byrne)
#### Post date: [March 11, 2016, 11:51am UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896/5 "2016-03-11T11:51:35Z")

</div>

Boom! (thanks BTW )

```
ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "1,2"
LINE 1: ...ERE ("posts"."deleted_at" IS NULL) AND (user_id = '1,2' or t...
                                                             ^
: SELECT "posts".* FROM "posts" WHERE ("posts"."deleted_at" IS NULL) AND (user_id = '1,2' or topic_id = '4') ORDER BY "posts"."created_at" DESC LIMIT 30)
/var/lib/gems/2.2.0/gems/rack-mini-profiler-0.9.9.2/lib/patches/db/pg.rb:93:in `exec'

```

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [March 11, 2016, 2:12pm UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896/6 "2016-03-11T14:12:49Z")

</div>

Try `Post.where(user_id IN (?) OR topic_id IN (?), [1,2], [4,5])`

---

<div class="post-metadata">

### Author: ![Huggingfluffybear](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/huggingfluffybear/32/60209_2.png) [@Huggingfluffybear](https://meta.discourse.org/u/Huggingfluffybear)
#### Post date: [March 11, 2016, 2:13pm UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896/7 "2016-03-11T14:13:56Z")

</div>

So if I say post.where() it causes a error?

---

<div class="post-metadata">

### Author: ![Andrew\_Byrne](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/andrew_byrne/32/116413_2.png) [@Andrew\_Byrne](https://meta.discourse.org/u/Andrew_Byrne)
#### Post date: [March 11, 2016, 2:33pm UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896/8 "2016-03-11T14:33:42Z")

</div>

@tgxworld Thanks that helped a bunch.

For the record I was doing a bunch of stuff that I SHOULD have known better…

1. not splitting the string to an actual array (duh _facepalm_ )
2. your request seems to have fix the syntax issues (woot)

final code

```
uid = params["uid"].split(",")
tid = params["tid"].split(",")

posts = Post.where('user_id IN (?) OR topic_id IN (?)', uid, tid )
			.order(created_at: :desc)
			.limit(params["total"])
			.offset(params["start"])

obj = {follow_posts: posts }
render json: obj

```

Massive thanks!! 😃

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [March 14, 2016, 12:07pm UTC](https://meta.discourse.org/t/post-where-causing-500-error/40896/9 "2016-03-14T12:07:37Z")

</div>

This topic was automatically closed after 2 days. New replies are no longer allowed.
