0

For some reason my query is now returning an array of hashes when I need an array of objects. I can't figure out what to do.

inbox_sql = self.mailbox.inbox.where('conversations.id IN (?)', active_conversations).reorder('').to_sql
sentbox_sql = self.mailbox.sentbox.reorder('').to_sql

sql = "#{inbox_sql} UNION #{sentbox_sql}"
conversations = Profile.connection.execute(sql)

When

self.inbox.mailbox.first.class

returns

Conversation(id: integer, subject: string, created_at: datetime, updated_at: datetime)

BUT

ActiveRecord::Base.connection.execute(sql).first.class

returns hash, which is problematic and ruining the rest of my code in the views. [ Used to return a Conversation object in a less optimal way], but I need it to return a Conversation(..) object

1
  • Do you really need all the UNION and to_sql stuff ? It seems too complicated and may be rewritten with a simple where that would return Conversations. What are the relation between self, mailbox and inbox/sentbox ? Commented Jun 25, 2014 at 21:25

1 Answer 1

1

In rails you don't execute things, you use rails ActiveRecord functions.

read about it here: http://guides.rubyonrails.org/active_record_querying.html

for example:

Conversation.where(...)

will execute a sql query and will return Conversation instances

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.