ActiveRecord::StatementInvalid in Orders::BuildController#update - ruby-on-rails

Working with these models:
User.rb
class User < ApplicationRecord
has_many :quotes
end
Quote.rb
class Quote < ApplicationRecord
belongs_to :user
has_many :country_quotes, dependent: :delete_all
has_many :countries, through: :country_quotes, dependent: :delete_all
end
Country.rb
class Country < ApplicationRecord
has_many :country_quotes, dependent: :delete_all
has_many :quotes, through: :country_quotes, dependent: :delete_all
end
Country_Quote.rb
class CountryQuote < ApplicationRecord
belongs_to :country, dependent: :delete
belongs_to :quote, dependent: :delete
end
Via console, I can run this query and I get the result I'm looking for:
theresults = Quote.joins(:countries).where("countries.name = 'España'")
theresults.where(shippingtype: 'Urgente').first.cost
However when I try to run a similar query within build_controller.rb (it's a controller to handle my Wicked gem for the model Order) I get this error:
ActiveRecord::StatementInvalid in Orders::BuildController#update
PG::SyntaxError: ERROR: syntax error at or near "order" LINE 1: ...RE "quotes"."user_id" = $1 AND (countries.name = #order.coun... ^
class Orders::BuildController < ApplicationController
def update
temporderregularshippingprice = #user.quotes.joins(:countries).where("countries.name = #order.country" )
puts 'user.quotes2: ' + #user.quotes
puts 'order.country2: ' + #order.country
orderregularshippingprice = temporderregularshippingprice.where(shippingtype: 'Ordinario').first.cost # line returning the error
puts 'orderregularshippingprice: ' + orderregularshippingprice.to_s
...
end
end
According to the trace, #user.quotes and #order.country are correctly detected from that part of the code:
11:04:21 web.1 | ↳ app/controllers/orders/build_controller.rb:52:in `update'
11:04:21 web.1 | orderpriceperpage:
11:04:21 web.1 | **user.quotes2**: #<Quote::ActiveRecord_Associations_CollectionProxy:0x00000001132da8c0>
11:04:21 web.1 | **order.country2**: España
11:04:21 web.1 | Quote Load (0.6ms) SELECT "quotes".* FROM "quotes" INNER JOIN "country_quotes" ON "country_quotes"."quote_id" = "quotes"."id" INNER JOIN "countries" ON "countries"."id" = "country_quotes"."country_id" WHERE "quotes"."user_id" = $1 AND (countries.name = #order.country) AND "quotes"."shippingtype" = $2 ORDER BY "quotes"."id" ASC LIMIT $3 [["user_id", 2], ["shippingtype", "Ordinario"], ["LIMIT", 1]]
11:04:21 web.1 | ↳ app/controllers/orders/build_controller.rb:62:in `update'
11:04:21 web.1 | Completed 500 Internal Server Error in 43ms (ActiveRecord: 13.8ms | Allocations: 22630)
11:04:21 web.1 |
11:04:21 web.1 |
11:04:21 web.1 |
11:04:21 web.1 | ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: syntax error at or near "order"
11:04:21 web.1 | LINE 1: ...RE "quotes"."user_id" = $1 AND (countries.name = #order.coun...
11:04:21 web.1 | ^
11:04:21 web.1 | ):
11:04:21 web.1 |

You are using the variable in double quotes which is interpreted as string.
temporderregularshippingprice = #user.quotes.joins(:countries).where("countries.name = #order.country" )
Use this instead
temporderregularshippingprice = #user.quotes.joins(:countries).where("countries.name = ?", #order.country )
OR
temporderregularshippingprice = #user.quotes.joins(:countries).where(countries: { name: #order.country })

Related

ActiveStorage: Ordering ActiveRecord Models by attachment Filename (ERROR missing FROM-clause entry for table "active_storage_blobs")

I am building a Rails app with a webpage that shows a list of QuoteDocuments sorted by the quotedocument's PDF created date, which is attached via ActiveStorage
class Quote < ApplicationRecord
has_many :quote_documents, dependent: :destroy
end
class QuoteDocument < ApplicationRecord
has_one_attached :document
end
This is the line of code I am using that gives me an error
#quote.quote_documents.includes(:document_attachment).order('active_storage_blobs.filename ASC')
And this is the error
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: missing FROM-clause entry for table "active_storage_blobs")
And the SQL statement
: SELECT "quote_documents"."id" AS t0_r0, "quote_documents"."quote_id" AS t0_r1, "quote_documents"."version" AS t0_r2, "quote_documents"."created_at" AS t0_r3, "quote_documents"."updated_at" AS t0_r4, "quote_documents"."document_file_name" AS t0_r5, "quote_documents"."document_content_type" AS t0_r6, "quote_documents"."document_file_size" AS t0_r7, "quote_documents"."document_updated_at" AS t0_r8, "quote_documents"."document_type" AS t0_r9, "quote_documents"."description" AS t0_r10, "quote_documents"."user_id" AS t0_r11, "active_storage_attachments"."id" AS t1_r0, "active_storage_attachments"."name" AS t1_r1, "active_storage_attachments"."record_type" AS t1_r2, "active_storage_attachments"."record_id" AS t1_r3, "active_storage_attachments"."blob_id" AS t1_r4, "active_storage_attachments"."created_at" AS t1_r5 FROM "quote_documents" LEFT OUTER JOIN "active_storage_attachments" ON "active_storage_attachments"."record_id" = "quote_documents"."id" AND "active_storage_attachments"."record_type" = $1 AND "active_storage_attachments"."name" = $2 WHERE "quote_documents"."quote_id" = $3 AND "quote_documents"."document_type" = $4 ORDER BY active_storage_blobs.filename ASC LIMIT $5
Any idea what I should be doing to fix this?
You need to use include in your original statement that sets the #quote, rather than from the #quote variable:
#quote = Quote.includes(quote_documents: {document_attachment: :blob}).find(params[id])
in your controller
then in your view you should just be able to use:
#quote.quote_documents.order('active_storage_blobs.filename ASC')

Ruby Twitter gem: undefined method `each_byte' for nil:NilClass on twitter_client.update

I have wierd problem with gem. It is working perfectly fine in rails console, but from lib/ it isnt
undefined method `each_byte' for nil:NilClass
in a row
twitter_client.update(tweet_text.to_s)
def twitter_client
Twitter::REST::Client.new do |config|
config.consumer_key = credentials[:consumer_key]
config.consumer_secret = credentials[:consumer_secret]
config.access_token = credentials[:access_token]
config.access_token_secret = credentials[:access_token_secret]
config.user_agent = "TwitterRubyGem/6.2.0"
end
end
full_trace:
| 2019-07-23T16:53:21.793Z 64165 TID-oxprcg2np WARN: NoMethodError: undefined method `each_byte' for nil:NilClass
| 2019-07-23T16:53:21.793Z 64165 TID-oxprcg2np WARN: /Users/DekaKisaLove/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/uri/rfc2396_parser.rb:308:in `block in escape'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/activesupport-5.2.3/lib/active_support/core_ext/string/output_safety.rb:230:in `gsub'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/activesupport-5.2.3/lib/active_support/core_ext/string/output_safety.rb:230:in `gsub'
| /Users/DekaKisaLove/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/uri/rfc2396_parser.rb:305:in `escape'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:32:in `escape'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:114:in `block (2 levels) in normalized_params'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:114:in `collect'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:114:in `block in normalized_params'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:114:in `collect'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:114:in `normalized_params'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:110:in `signature_base'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:101:in `hmac_sha1_signature'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:97:in `signature'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:77:in `signed_attributes'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:83:in `normalized_attributes'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/simple_oauth-0.3.1/lib/simple_oauth/header.rb:65:in `to_s'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/headers.rb:39:in `auth_header'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/headers.rb:30:in `request_headers'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/rest/request.rb:62:in `set_multipart_options!'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/rest/request.rb:28:in `initialize'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:50:in `new'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:50:in `perform_request'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:72:in `perform_request_with_object'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:64:in `perform_post_with_object'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/rest/tweets.rb:155:in `update!'
| /Users/DekaKisaLove/.rvm/gems/ruby-2.6.1#closedwon/gems/twitter-6.2.0/lib/twitter/rest/tweets.rb:126:in `update'
rfc2396_parser.rb:308
def escape(str, unsafe = #regexp[:UNSAFE])
unless unsafe.kind_of?(Regexp)
# perhaps unsafe is String object
unsafe = Regexp.new("[#{Regexp.quote(unsafe)}]", false)
end
str.gsub(unsafe) do
us = $&
tmp = ''
us.each_byte do |uc|
tmp << sprintf('%%%02X', uc)
end
tmp
end.force_encoding(Encoding::US_ASCII)
end
Im very sorry, problem was in tweet_text - it CANNOT HAVE special symbols. I hope this helps someone someday... I spent 4 hrs.

Update object identified by composite key with rails

I have actually a table with components. Each component have a specific version like it is describe above (there are other attributes like name, component_type_id, ...):
id | version_id
-----+------------
167 | 1
167 | 96
167 | 97
167 | 98
167 | 99
166 | 1
166 | 92
The attributes "id" and "version_id" are a composite primary key, and I want to edit a object by identifying this composite PK.
If I want to update the version 99 of the component #167 =>
With rails when I do :
Component.where(id: 167, version_id: 99).first.update({"component_type_id"=>"5", "name"=>"CCC"})
Rails do that :
SELECT "components".* FROM "components" WHERE "components"."id" = $1 AND "components"."version_id" = $2 ORDER BY "components"."id" ASC LIMIT 1 [["id", 167], ["version_id", 99]]
UPDATE "components" SET "name" = $1, "updated_at" = $2 WHERE "components"."id" = 167 [["name", "CCC"], ["updated_at", "2016-04-19 08:05:09.049345"]]
But I want something like that :
UPDATE "components" SET "name" = $1, "updated_at" = $2 WHERE "components"."id" = 167 AND version_id = 99 [["name", "CCC"], ["updated_at", "2016-04-19 08:05:09.049345"]]
Thanks to help me
Cheers
Since these is only one record with the given id and version, Try:
Component.where(id: 167, version_id: 99).update_all(component_type_id: 5, name: "CCC")
In this way, Rails will not instance a record, avoid your problem.

jruby rails4 eager_load doesn't work

I have two models:
class Transaction < ActiveRecord::Base
self.table_name = 'DEPOSIT_TRANSACTIONS'
belongs_to :deposit_account
scope :not_test, -> { includes(:deposit_account).where("deposit_accounts.is_test_account is null or deposit_accounts.is_test_account != 1").references(:deposit_account) }
...
end
class DepositAccount < ActiveRecord::Base
has_many :transactions, -> { order(:creation_date => :desc) }
default_scope { where(:purpose => 'COUNTERFEIT') }
scope :not_test, -> { where("is_test_account != 1") }
...
end
I tried to eager load the both tables with:
irb(main):008:0> request_transactions = Transaction.eager_load(:deposit_account)
**I polish the following output**
SQL (6390.0ms) SELECT "DEPOSIT_TRANSACTIONS"."ID" AS t0_r0,
"DEPOSIT_TRANSACTIONS"."DEPOSIT_ACCOUNT_ID" AS t0_r1,
"DEPOSIT_TRANSACTIONS"."TRANSACTION_TYPE" AS t0_r2,
"DEPOSIT_TRANSACTIONS"."TRACKING_ID" AS t0_r3,
"DEPOSIT_TRANSACTIONS"."AMOUNT" AS t0_r4,
"DEPOSIT_TRANSACTIONS"."STATUS" AS t0_r5,
"DEPOSIT_TRANSACTIONS"."TRANSACTION_COMMENT" AS t0_r6,
"DEPOSIT_TRANSACTIONS"."NOTIFICATION_EMAILS" AS t0_r7,
"DEPOSIT_TRANSACTIONS"."CREATED_BY" AS t0_r8,
"DEPOSIT_TRANSACTIONS"."CREATION_DATE" AS t0_r9,
"DEPOSIT_TRANSACTIONS"."LAST_UPDATED_BY" AS t0_r10,
"DEPOSIT_TRANSACTIONS"."LAST_UPDATED_DATE" AS t0_r11,
"DEPOSIT_TRANSACTIONS"."FINISH_DATE" AS t0_r12,
"DEPOSIT_TRANSACTIONS"."PAYMENT_DATE" AS t0_r13,
"DEPOSIT_TRANSACTIONS"."MATCH_TYPE" AS t0_r14,
"DEPOSIT_TRANSACTIONS"."ADDITIONAL_INFO" AS t0_r15,
"DEPOSIT_ACCOUNTS"."ID" AS t1_r0,
"DEPOSIT_ACCOUNTS"."SELLER_ID" AS t1_r1,
"DEPOSIT_ACCOUNTS"."MARKETPLACE_ID" AS t1_r2,
"DEPOSIT_ACCOUNTS"."PURPOSE" AS t1_r3,
"DEPOSIT_ACCOUNTS"."TOTAL_DEPOSIT_BALANCE" AS t1_r4,
"DEPOSIT_ACCOUNTS"."OUTSTANDING_DEPOSIT_REQUEST" AS t1_r5,
"DEPOSIT_ACCOUNTS"."OUTSTANDING_REFUND_REQUEST" AS t1_r6,
"DEPOSIT_ACCOUNTS"."CREATED_BY" AS t1_r7,
"DEPOSIT_ACCOUNTS"."CREATION_DATE" AS t1_r8,
"DEPOSIT_ACCOUNTS"."LAST_UPDATED_BY" AS t1_r9,
"DEPOSIT_ACCOUNTS"."LAST_UPDATED_DATE" AS t1_r10,
"DEPOSIT_ACCOUNTS"."IS_TEST_ACCOUNT" AS t1_r11,
"DEPOSIT_ACCOUNTS"."RECORD_VERSION_NUMBER" AS t1_r12,
"DEPOSIT_ACCOUNTS"."OUTSTANDING_CONFISCATE_REQUEST" AS t1_r13
FROM "DEPOSIT_TRANSACTIONS"
LEFT OUTER JOIN "DEPOSIT_ACCOUNTS"
ON "DEPOSIT_ACCOUNTS"."ID" = "DEPOSIT_TRANSACTIONS"."DEPOSIT_ACCOUNT_ID"
AND "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT'
DepositAccount Load (235.0ms) SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 143 AND ROWNUM <= 1
DepositAccount Load (535.0ms) SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 143 AND ROWNUM <= 1
DepositAccount Load (471.0ms) SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 147 AND ROWNUM <= 1
DepositAccount Load (237.0ms) SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 138 AND ROWNUM <= 1
...
But it generates so many SQL, which means rails doesn't load eagerly I think. I expected the first clause is enough. Please help to eager load the tables. Thanks.
My environment:
jruby 1.7
rails 4.0
EDIT1:
I tried to run the same code on ruby 1.9 rails 4.0, the eager load works fine. So I guess this is caused by JRuby. Does anyone else have this problem?
Don't know if it matters, I use activerecord-oracle_enhanced-adapter in ruby 1.9 env, but JDBC driver in
JRuby. The database it oracle.
The problem is solved by upgrading activerecord from activerecord-4.0.9 to activerecord-4.1.5. BTW, I use activerecord-jdbc-adapter-1.3.7 as the adapter.

Can't Join a model when using belongs_to

class Exercise < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :exercises
end
Why can't I find all Exercises there were created by users with the role of "admin"?
Exercise.first :include => [:user], :conditions => ['user.role = "admin"']
The error shoes that the users table is not being joined.
Exercise.first :include => [:user], :conditions => ['user.role = "admin"']
Exercise Load Including Associations (0.0ms) Mysql::Error: Unknown column 'user.role' in 'where clause': SELECT `exercises`.`id` AS t0_r0, `exercises`.`title` AS t0_r1, `exercises`.`description` AS t0_r2, `exercises`.`fitness_type` AS t0_r3, `exercises`.`trackables` AS t0_r4, `exercises`.`user_id` AS t0_r5, `exercises`.`created_at` AS t0_r6, `exercises`.`updated_at` AS t0_r7, `users`.`id` AS t1_r0, `users`.`created_at` AS t1_r1, `users`.`updated_at` AS t1_r2, `users`.`first_name` AS t1_r3, `users`.`last_name` AS t1_r4, `users`.`email` AS t1_r5, `users`.`crypted_password` AS t1_r6, `users`.`password_salt` AS t1_r7, `users`.`persistence_token` AS t1_r8, `users`.`single_access_token` AS t1_r9, `users`.`perishable_token` AS t1_r10, `users`.`login_count` AS t1_r11, `users`.`failed_login_count` AS t1_r12, `users`.`last_request_at` AS t1_r13, `users`.`current_login_at` AS t1_r14, `users`.`last_login_at` AS t1_r15, `users`.`current_login_ip` AS t1_r16, `users`.`last_login_ip` AS t1_r17, `users`.`role` AS t1_r18, `users`.`avatar_file_name` AS t1_r19, `users`.`avatar_content_type` AS t1_r20, `users`.`avatar_file_size` AS t1_r21, `users`.`avatar_updated_at` AS t1_r22 FROM `exercises` LEFT OUTER JOIN `users` ON `users`.id = `exercises`.user_id WHERE (user.role = "admin") LIMIT 1
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'user.role' in 'where clause': SELECT `exercises`.`id` AS t0_r0, `exercises`.`title` AS t0_r1, `exercises`.`description` AS t0_r2, `exercises`.`fitness_type` AS t0_r3, `exercises`.`trackables` AS t0_r4, `exercises`.`user_id` AS t0_r5, `exercises`.`created_at` AS t0_r6, `exercises`.`updated_at` AS t0_r7, `users`.`id` AS t1_r0, `users`.`created_at` AS t1_r1, `users`.`updated_at` AS t1_r2, `users`.`first_name` AS t1_r3, `users`.`last_name` AS t1_r4, `users`.`email` AS t1_r5, `users`.`crypted_password` AS t1_r6, `users`.`password_salt` AS t1_r7, `users`.`persistence_token` AS t1_r8, `users`.`single_access_token` AS t1_r9, `users`.`perishable_token` AS t1_r10, `users`.`login_count` AS t1_r11, `users`.`failed_login_count` AS t1_r12, `users`.`last_request_at` AS t1_r13, `users`.`current_login_at` AS t1_r14, `users`.`last_login_at` AS t1_r15, `users`.`current_login_ip` AS t1_r16, `users`.`last_login_ip` AS t1_r17, `users`.`role` AS t1_r18, `users`.`avatar_file_name` AS t1_r19, `users`.`avatar_content_type` AS t1_r20, `users`.`avatar_file_size` AS t1_r21, `users`.`avatar_updated_at` AS t1_r22 FROM `exercises` LEFT OUTER JOIN `users` ON `users`.id = `exercises`.user_id WHERE (user.role = "admin") LIMIT 1
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:in `execute'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:608:in `select'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1617:in `select_all_rows'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1395:in `find_with_associations'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1393:in `catch'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1393:in `find_with_associations'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1546:in `find_every'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1505:in `find_initial'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:613:in `find'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:623:in `first'
from (irb):7
>>
I know I can do this in a 2 step process by first finding all admin users then find exercises by "user_id" but want to make this into a named_scope.
I am pretty sure it is
Exercise.first :include => [:user], :conditions => ['users.role = "admin"']
instead of
Exercise.first :include => [:user], :conditions => ['user.role = "admin"']
Your condition should have users as plural: users.role

Resources