ActiveRecord_Relation的未定义方法范围

时间:2016-06-15 12:44:13

标签: ruby-on-rails

为什么我定义的范围不起作用? 我的代码的一部分如下:

users habtm messages
projects habtm messages

Message.rb

scope :new_messages,-> { where(msg_status: 'new') }
scope :without_project,-> { includes(:messages_projects).where('messages_projects.project_id' => nil) } #<---this one

def mymethod
  new_messages = Message.new_messages
  @users = User.active.joins(:messages).merge(Message.new_messages).uniq
  @users.each do |user|
   new_users_messages = new_messages.without_projects.includes(:messages_users).where('messages_users.user_id' => user.id)
  end
end

但如果我直接进行查询,则可以:

new_users_messages = new_messages.includes(:messages_projects).where('messages_projects.project_id' => nil).includes(:messages_users).where('messages_users.user_id' => user.id)

有人可以向我解释为什么会这样吗?

更新 这是日志,但没有什么有趣的

User Load (20.0ms)  SELECT DISTINCT "users".* FROM "users" INNER JOIN "accounts" ON "accounts"."user_id" = "users"."id" INNER JOIN "messages_users" ON "messages_users"."user_id" = "users"."id" INNER JOIN "messages" ON "messages"."id" = "messages_users"."message_id" WHERE "accounts"."blocked" = $1 AND "messages"."msg_status" = $2 AND "accounts"."email_notify" = $3  [["blocked", "f"], ["msg_status", "new"], ["email_notify", "t"]]
  Message Load (0.5ms)  SELECT "messages".* FROM "messages" WHERE "messages"."msg_status" = $1  [["msg_status", "new"]]
Completed 500 Internal Server Error in 3116ms (ActiveRecord: 38.0ms)

NoMethodError (undefined method `without_projects' for #<Message::ActiveRecord_Relation:0x0000000a805fc0>):
  app/models/message.rb:24:in `block in sender'
  app/models/message.rb:23:in `sender'
  app/controllers/groups_controller.rb:47:in `index'


  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (19.6ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.3ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (36.4ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.7ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.5ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.4ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (30.8ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.6ms)
  Rendered /.rvm/gems/ruby-2.0.0-p647/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (78.9ms)

0 个答案:

没有答案