MySQL查询不起作用

时间:2012-06-12 06:39:43

标签: mysql ruby-on-rails

在我的网站上,用户有一个“待办事项”列表。在获得测验的最低成就水平后,该测验将从用户的待办事项中删除。以下代码适用于产品,但不适用于通过“BusinessRelationship”列出的任何测验。

def not_quizzed_things
   taken_company_ids = quizzes_taken.select{|q| q.thing_type == 'BusinessRelationship'}.map{|q| q.thing_id}.uniq
   conds = "business_relationships.from_company_id = ? and (business_relationships.legit_quiz_questions_count + c.legit_quiz_questions_count) > 0"
   args = [company_id]
   unless taken_company_ids.blank?
     conds += " and c.id not in (?)"
     args << taken_company_ids
  end

  quiz_companies = BusinessRelationship.find(:all, :joins => "join companies c on c.id = business_relationships.to_company_id", :conditions => [conds]+args, :order => "id", :limit => 0)
  quiz_companies = [company] + quiz_companies unless company.legit_quiz_questions_count <= 0 || quizzes_taken.select{|q| q.thing_type == 'Company'}.map{|q| q.thing}.include?(company)
  n = 20 - quiz_companies.size

  taken_products_retailer_ids = quizzes_taken.select{|q| q.thing_type == 'ProductsRetailer'}.map{|q| q.thing_id}.uniq
  conds = "products_retailers.company_id = ? and (p.legit_quiz_questions_count + products_retailers.legit_quiz_questions_count) > 0"
  args = [company_id]
  unless taken_products_retailer_ids.blank?
    conds += " and products_retailers.id not in (?)"
    args << taken_products_retailer_ids
  end
quiz_products_retailers = ProductsRetailer.find(:all, :joins => "join products p on p.id = products_retailers.product_id", :conditions => [conds]+args, :order => "id", :limit => n)
quiz_companies + quiz_products_retailers
end

控制台结果:

ruby-1.8.7-p352 :007 > y u.quizzes_taken.last
--- !ruby/object:Quiz 
attributes: 
  thing_id: "213"
  created_at: 2012-06-09 20:07:32
  end_time: 2012-06-09 20:07:56
  current_attempt_id: 
  thing_type: BusinessRelationship
  updated_at: 2012-06-09 20:07:56
  order: "2388,2389,2390,2391"
  id: "467"
  correct_count: "4"
  user_id: "392"
  attempt_count: "4"
  start_time: 2012-06-09 20:07:32
attributes_cache: {}

非常感谢任何想法或建议!

1 个答案:

答案 0 :(得分:0)

我会查看您的调试窗口以查看发送到数据库的确切SQL,并查看您的查询未按预期发布的原因。从数据库回到代码。