1

I have a Deal model with a lot of associations. One of the associations is Currency. The deals table and the currencies table both have a name column. Now, I have the following ActiveRecord query:

Deal.
  joins(:currency).
  where("privacy = ? or user_id = ?", false, doorkeeper_token.resource_owner_id).
  select("deals.name as deal_name, deals.date as deal_creation_date, deals.amount as deal_amount, currencies.name as currency_name, currencies.symbol as currency_symbol")

This query doesn't work, its result is an array of Deal objects with no attributes. According to someone on IRC, the "as" parts are incorrect because the ORM doesn't know how to assign the columns to which attribute (or something like that) which is fair enough. I tried to add attr_accessor and attr_accessible clauses though but it didn't work.

How can I make the above query work please? What I expect the result to be is an Array of Deal objects with deal_name, deal_creation_date, etc. virtual attributes.

tee
  • 4,149
  • 1
  • 32
  • 44
Robert Audi
  • 8,019
  • 9
  • 45
  • 67

1 Answers1

2

Most likely the query is working correctly, but the returned Deal objects appear not to have any attributes when you print them out because of the way that Deal implements the inspect method.

So if you assign the result of the query to a variable you will see that this appears empty:

v.each do |deal| ; puts deal.inspect ; end

While this shows the attributes you want:

v.each do |deal| ; puts deal.to_yaml ; end

More details are in this question.

Community
  • 1
  • 1
Derek Hill
  • 5,965
  • 5
  • 55
  • 74
  • Okay that works for me. Except for associations/joins. For example, take this query: `Deal.joins(:currency).where("deals.privacy = ? or deals.user_id = ?", false, 4).select("deals.name as deal_name, deals.date as deal_date, deals.amount as deal_amount, currencies.symbol as currency").as_json`. In this example `currency` is `nil` even though it shouldn't be (since it has a value in the DB). However, its value is a special character. Is that the problem? – Robert Audi May 10 '13 at 17:03
  • It could be that special characters are causing the problem with currency. You could try putting `# encoding: UTF-8` at the top of the file. [Here is an example](http://stackoverflow.com/a/6916663/1450420) of somebody solving what might be a similar problem – Derek Hill May 10 '13 at 18:02
  • What I don't understand is that everything works fine if I don't use "as" in the select statement (including the special character in the json). So I doubt that the problem's origin is the special characters... – Robert Audi May 10 '13 at 18:14
  • Hmmm. I'm afraid I don't know why that might be. – Derek Hill May 10 '13 at 18:24
  • 1
    Ok I managed to (kind of) pinpoint the problem. If I use this query: `Deal.joins(:currency).where("privacy = ? or user_id = ?", false, 1).select("currencies.symbol as currency").as_json` all the currencies are nil, but if I use that query: `Deal.joins(:currency).where("privacy = ? or user_id = ?", false, 1).select("currencies.symbol as currency_symbol").as_json` everything works fine. It's probably an ambiguity problem with the association. Makes sense. – Robert Audi May 11 '13 at 10:22