# Has many association with finder\_sql

**URL:** <https://discuss.rubyonrails.org/t/has-many-association-with-finder-sql/56486>\
**Category:** rubyonrails-talk\
**Created:** [December 28, 2010, 9:40pm UTC](https://discuss.rubyonrails.org/t/has-many-association-with-finder-sql/56486 "2010-12-28T21:40:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 28, 2010, 9:40pm UTC](https://discuss.rubyonrails.org/t/has-many-association-with-finder-sql/56486/1 "2010-12-28T21:40:53Z")

</div>

Hi, I have a customer and conversation model. A conversation is between 2 customers. Here is my association in customer. has\_many :conversations, :class\_name =\> "Conversation", :order =\> 'updated\_at DESC', :finder\_sql =\> 'SELECT \* FROM conversations c WHERE (c.started\_by = #{id} OR c.with = #{id})' do &nbsp;&nbsp;&nbsp;&nbsp;def with(cust\_id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all(:conditions =\> ["conversations.started\_by = ? OR conversations.with = ?", cust\_id, cust\_id]) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

Inside the association block I made a method which filters on the second customer of the conversation.

But when I do: myCustomer.conversations.with(someoneelseID)

I get the following error.

ActiveRecord::StatementInvalid (Mysql::Error: Operand should contain 1 column(s): SELECT \* FROM `conversations` WHERE (conversations.started\_by = 1 OR conversations.with = 1) AND (SELECT \* FROM conversations c WHERE (c.started\_by = 1 OR c.with = 1)) ORDER BY updated\_at DESC)

How can i make Rails build a correct sql request? Greg

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 30, 2010, 10:45pm UTC](https://discuss.rubyonrails.org/t/has-many-association-with-finder-sql/56486/2 "2010-12-30T22:45:34Z")

</div>

Greg Ma wrote in post #971151:

> ActiveRecord::StatementInvalid (Mysql::Error: Operand should contain 1 column(s): SELECT \* FROM `conversations` WHERE (conversations.started\_by = 1 OR conversations.with = 1) AND (SELECT \* FROM conversations c WHERE (c.started\_by = 1 OR c.with = 1)) ORDER BY updated\_at DESC)

Are you sure you really need :finder\_sql? I didn't see anything here that :condtions and :order couldn't handle.
