2014-03-12 39 views
0

>----<表示has_many_and_belongs_to在這篇文章中。heroku中的PG問題和型號排序w.r.t選擇了導軌中的ID

讓我開始與模型和關係:

事件> ----- <類別,事件> ----- <域,事件> ----- < eligibles

我正在根據所做的選擇在refresh_controller中整理事件數據。這是我使用的代碼。它與sqlite3完美配合。在生產中,PG正在引發一些問題。當我有沒有鏈接到事件的類別並執行搜索時,它會拋出一個錯誤。讓我知道是否有另一種方式來做到這一點。

這裏是控制器。

def index 
@cat = Category.find(params[:category][:category_id]) if params[:category][:category_id].present? 
@dom = Domain.find(params[:domain][:domain_id]) if params[:domain][:domain_id].present? 
@eli = Eligible.find(params[:eligible][:eligible_id]) if params[:eligible][:eligible_id].present? 
@e_1=[] 
@e_2=[] 
@e_3=[] 
@e_1 << @cat.events.collect{|p| p.id} if @cat.present? 
@e_2 << @dom.events.collect{|p| p.id} if @dom.present? 
@e_3 << @eli.events.collect{|p| p.id} if @eli.present? 
@e_f = @e_1 | @e_2 | @e_3 
@all = Event.where(id: @e_f) 
@user = current_user || User.new 

#@event_ids || @events.collect{|p| p.id} 
#@event_ids = id_array.collect{|id| id.to_i} 

end 

index.html.erb:

<div class="container"> 
    <b> <%= @cat.name if @cat.present? %> <%= @dom.name if @dom.present? %> <%= @eli.name if @eli.present? %></b> 
<%= render partial: "static_pages/event", collection: @all %> 

</div> 

這裏是我的Heroku的日誌:

 heroku logs 
2014-03-12T09:39:28.894581+00:00 app[web.1]: /app/vendor/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:170:in `select' 
2014-03-12T09:39:28.894581+00:00 app[web.1]: [2014-03-12 09:39:28] FATAL SignalException: SIGTERM 
2014-03-12T09:39:28.894581+00:00 app[web.1]: /app/vendor/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:160:in `start' 
2014-03-12T09:39:28.894581+00:00 app[web.1]: /app/vendor/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:170:in `block in start' 
2014-03-12T09:39:28.894581+00:00 app[web.1]: /app/vendor/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:32:in `start' 
2014-03-12T09:39:28.894581+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/handler/webrick.rb:14:in `run' 
2014-03-12T09:39:28.894834+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:71:in `<top (required)>' 
2014-03-12T09:39:28.894834+00:00 app[web.1]: bin/rails:4:in `require' 
2014-03-12T09:39:28.894834+00:00 app[web.1]: bin/rails:4:in `<main>' 
2014-03-12T09:39:30.273961+00:00 heroku[web.1]: Process exited with status 143 
2014-03-12T09:39:33.095928+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 5234 -e production` 
2014-03-12T09:39:38.514112+00:00 app[web.1]: [2014-03-12 09:39:38] INFO WEBrick 1.3.1 
2014-03-12T09:39:38.514112+00:00 app[web.1]: [2014-03-12 09:39:38] INFO ruby 2.1.0 (2013-12-25) [x86_64-linux] 
2014-03-12T09:39:38.514323+00:00 app[web.1]: [2014-03-12 09:39:38] INFO WEBrick::HTTPServer#start: pid=2 port=5234 
2014-03-12T09:39:41.211555+00:00 heroku[web.1]: Stopping all processes with SIGTERM 
2014-03-12T09:39:41.702711+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/server.rb:264:in `start' 
2014-03-12T09:39:41.702711+00:00 app[web.1]: [2014-03-12 09:39:41] FATAL SignalException: SIGTERM 
2014-03-12T09:39:41.702711+00:00 app[web.1]: /app/vendor/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:170:in `select' 
2014-03-12T09:39:41.702711+00:00 app[web.1]: /app/vendor/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:32:in `start' 
2014-03-12T09:39:41.702711+00:00 app[web.1]: /app/vendor/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:170:in `block in start' 
2014-03-12T09:39:41.702711+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/handler/webrick.rb:14:in `run' 
2014-03-12T09:39:41.702711+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands/server.rb:84:in `start' 
2014-03-12T09:39:41.702976+00:00 app[web.1]: [2014-03-12 09:39:41] INFO going to shutdown ... 
2014-03-12T09:39:41.702711+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:71:in `tap' 
2014-03-12T09:39:41.702976+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:71:in `<top (required)>' 
2014-03-12T09:39:41.702976+00:00 app[web.1]: bin/rails:4:in `require' 
2014-03-12T09:39:41.702976+00:00 app[web.1]: bin/rails:4:in `<main>' 
2014-03-12T09:39:41.702711+00:00 app[web.1]: /app/vendor/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:160:in `start' 
2014-03-12T09:39:41.702711+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:76:in `block in <top (required)>' 
2014-03-12T09:39:41.702976+00:00 app[web.1]: => Rails 4.0.2 application starting in production on http://0.0.0.0:5234 
2014-03-12T09:39:41.702976+00:00 app[web.1]: => Run `rails server -h` for more startup options 
2014-03-12T09:39:41.702976+00:00 app[web.1]: => Booting WEBrick 
2014-03-12T09:39:41.702976+00:00 app[web.1]: => Ctrl-C to shutdown server 
2014-03-12T09:39:41.702976+00:00 app[web.1]: Exiting 
2014-03-12T09:39:41.702976+00:00 app[web.1]: [2014-03-12 09:39:41] INFO WEBrick::HTTPServer#start done. 
2014-03-12T09:39:42.930276+00:00 heroku[web.1]: Process exited with status 143 
2014-03-12T09:39:51.611779+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 53475 -e production` 
2014-03-12T09:39:58.904726+00:00 app[web.1]: [2014-03-12 09:39:58] INFO WEBrick 1.3.1 
2014-03-12T09:39:58.904726+00:00 app[web.1]: [2014-03-12 09:39:58] INFO ruby 2.1.0 (2013-12-25) [x86_64-linux] 
2014-03-12T09:39:58.904963+00:00 app[web.1]: [2014-03-12 09:39:58] INFO WEBrick::HTTPServer#start: pid=2 port=53475 
2014-03-12T09:39:59.295118+00:00 heroku[web.1]: State changed from starting to up 
2014-03-12T09:40:00.368665+00:00 app[web.1]: => Run `rails server -h` for more startup options 
2014-03-12T09:40:00.368665+00:00 app[web.1]: Started GET "/refresh?utf8=%E2%9C%93&category%5Bcategory_id%5D=&domain%5Bdomain_id%5D=&eligible%5Beligible_id%5D=4&commit=Search" for 122.167.180.66 at 2014-03-12 09:40:00 +0000 
2014-03-12T09:40:00.368665+00:00 app[web.1]: => Booting WEBrick 
2014-03-12T09:40:00.368665+00:00 app[web.1]: => Rails 4.0.2 application starting in production on http://0.0.0.0:53475 
2014-03-12T09:40:00.368665+00:00 app[web.1]: => Ctrl-C to shutdown server 
2014-03-12T09:40:00.368665+00:00 app[web.1]: Started GET "/refresh?utf8=%E2%9C%93&category%5Bcategory_id%5D=&domain%5Bdomain_id%5D=&eligible%5Beligible_id%5D=4&commit=Search" for 122.167.180.66 at 2014-03-12 09:40:00 +0000 
2014-03-12T09:40:00.493767+00:00 app[web.1]: Processing by RefreshController#index as HTML 
2014-03-12T09:40:00.493767+00:00 app[web.1]: Processing by RefreshController#index as HTML 
2014-03-12T09:40:00.531632+00:00 app[web.1]: User Load (2.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 
2014-03-12T09:40:00.565344+00:00 app[web.1]: Eligible Load (3.0ms) SELECT "eligibles".* FROM "eligibles" WHERE "eligibles"."id" = $1 LIMIT 1 [["id", "4"]] 
2014-03-12T09:40:00.493974+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "category"=>{"category_id"=>""}, "domain"=>{"domain_id"=>""}, "eligible"=>{"eligible_id"=>"4"}, "commit"=>"Search"} 
2014-03-12T09:40:00.493974+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "category"=>{"category_id"=>""}, "domain"=>{"domain_id"=>""}, "eligible"=>{"eligible_id"=>"4"}, "commit"=>"Search"} 
2014-03-12T09:40:00.531632+00:00 app[web.1]: User Load (2.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 
2014-03-12T09:40:00.565344+00:00 app[web.1]: Eligible Load (3.0ms) SELECT "eligibles".* FROM "eligibles" WHERE "eligibles"."id" = $1 LIMIT 1 [["id", "4"]] 
2014-03-12T09:40:00.599548+00:00 app[web.1]: Event Load (1.6ms) SELECT "events".* FROM "events" WHERE "events"."id" IN() 
2014-03-12T09:40:00.592159+00:00 app[web.1]: Event Load (5.2ms) SELECT "events".* FROM "events" INNER JOIN "eligibles_events" ON "events"."id" = "eligibles_events"."event_id" WHERE "eligibles_events"."eligible_id" = $1 [["eligible_id", 4]] 
2014-03-12T09:40:00.599548+00:00 app[web.1]: Event Load (1.6ms) SELECT "events".* FROM "events" WHERE "events"."id" IN() 
2014-03-12T09:40:00.599548+00:00 app[web.1]: LINE 1: SELECT "events".* FROM "events" WHERE "events"."id" IN() 
2014-03-12T09:40:00.599548+00:00 a 
pp[web.1]: : SELECT "events".* FROM "events" WHERE "events"."id" IN() 
2014-03-12T09:40:00.599548+00:00 app[web.1]:                ^
2014-03-12T09:40:00.599548+00:00 app[web.1]: : SELECT "events".* FROM "events" WHERE "events"."id" IN() 
2014-03-12T09:40:00.600282+00:00 app[web.1]: Rendered refresh/index.html.erb within layouts/application (3.7ms) 
2014-03-12T09:40:00.599548+00:00 app[web.1]:                ^
2014-03-12T09:40:00.600282+00:00 app[web.1]: Rendered refresh/index.html.erb within layouts/application (3.7ms) 
2014-03-12T09:40:00.599548+00:00 app[web.1]: PG::Error: ERROR: syntax error at or near ")" 
2014-03-12T09:40:00.600424+00:00 app[web.1]: Completed 500 Internal Server Error in 106ms 
2014-03-12T09:40:00.592159+00:00 app[web.1]: Event Load (5.2ms) SELECT "events".* FROM "events" INNER JOIN "eligibles_events" ON "events"."id" = "eligibles_events"."event_id" WHERE "eligibles_events"."eligible_id" = $1 [["eligible_id", 4]] 
2014-03-12T09:40:00.599548+00:00 app[web.1]: LINE 1: SELECT "events".* FROM "events" WHERE "events"."id" IN() 
2014-03-12T09:40:00.602492+00:00 app[web.1]: ActionView::Template::Error (PG::Error: ERROR: syntax error at or near ")" 
2014-03-12T09:40:00.602492+00:00 app[web.1]:                ^
2014-03-12T09:40:00.602492+00:00 app[web.1]: : SELECT "events".* FROM "events" WHERE "events"."id" IN()): 
2014-03-12T09:40:00.602492+00:00 app[web.1]:  2: <b> <%= @cat.name if @cat.present? %> <%= @dom.name if @dom.present? %> <%= @eli.name if @eli.present? %></b> 
2014-03-12T09:40:00.602492+00:00 app[web.1]:  4: 
2014-03-12T09:40:00.602492+00:00 app[web.1]: LINE 1: SELECT "events".* FROM "events" WHERE "events"."id" IN() 
2014-03-12T09:40:00.602492+00:00 app[web.1]:  1: <div class="container"> 
2014-03-12T09:40:00.599548+00:00 app[web.1]: PG::Error: ERROR: syntax error at or near ")" 
2014-03-12T09:40:00.602693+00:00 app[web.1]:  6: <script type="text/javascript" language="javascript" charset="utf-8"> 
2014-03-12T09:40:00.602693+00:00 app[web.1]: app/views/refresh/index.html.erb:3:in `_app_views_refresh_index_html_erb___3814188212292096351_70209931602340' 
2014-03-12T09:40:00.602693+00:00 app[web.1]: 
2014-03-12T09:40:00.602693+00:00 app[web.1]: 
2014-03-12T09:40:00.602693+00:00 app[web.1]: ActionView::Template::Error (PG::Error: ERROR: syntax error at or near ")" 
2014-03-12T09:40:00.602693+00:00 app[web.1]:                ^
2014-03-12T09:40:00.600424+00:00 app[web.1]: Completed 500 Internal Server Error in 106ms 
2014-03-12T09:40:00.602693+00:00 app[web.1]:  1: <div class="container"> 
2014-03-12T09:40:00.602881+00:00 app[web.1]:  2: <b> <%= @cat.name if @cat.present? %> <%= @dom.name if @dom.present? %> <%= @eli.name if @eli.present? %></b> 
2014-03-12T09:40:00.602881+00:00 app[web.1]:  4: 
2014-03-12T09:40:00.602881+00:00 app[web.1]: 
2014-03-12T09:40:00.602693+00:00 app[web.1]: : SELECT "events".* FROM "events" WHERE "events"."id" IN()): 
2014-03-12T09:40:00.602492+00:00 app[web.1]:  3: <%= render partial: "static_pages/event", collection: @all %> 
2014-03-12T09:40:00.602693+00:00 app[web.1]: LINE 1: SELECT "events".* FROM "events" WHERE "events"."id" IN() 
2014-03-12T09:40:00.602881+00:00 app[web.1]:  3: <%= render partial: "static_pages/event", collection: @all %> 
2014-03-12T09:40:00.602881+00:00 app[web.1]:  5: </div> 
2014-03-12T09:40:00.602693+00:00 app[web.1]: 
2014-03-12T09:40:00.602881+00:00 app[web.1]:  6: <script type="text/javascript" language="javascript" charset="utf-8"> 
2014-03-12T09:40:00.602492+00:00 app[web.1]: 
2014-03-12T09:40:00.602881+00:00 app[web.1]: 
2014-03-12T09:40:00.602492+00:00 app[web.1]:  5: </div> 
2014-03-12T09:40:00.602881+00:00 app[web.1]: app/views/refresh/index.html.erb:3:in `_app_views_refresh_index_html_erb___3814188212292096351_70209931602340' 
2014-03-12T09:40:00.967239+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=floating-castle-4592.herokuapp.com request_id=d1fe2f58-7a19-42dc-9ef4-940d4841d822 fwd="122.167.180.66" dyno=web.1 connect=2ms service=7ms status=200 bytes=228 
2014-03-12T09:40:00.609749+00:00 heroku[router]: at=info method=GET path=/refresh?utf8=%E2%9C%93&category%5Bcategory_id%5D=&domain%5Bdomain_id%5D=&eligible%5Beligible_id%5D=4&commit=Search host=floating-castle-4592.herokuapp.com request_id=578810a1-52fc-4d2a-a095-97a8983d926b fwd="122.167.180.66" dyno=web.1 connect=1ms service=243ms status=500 bytes=1543 

編輯:

另外,我想知道如何將事件與他們在選擇中的受歡迎程度進行排序。

+0

你可以發佈更多你的堆棧跟蹤的錯誤?這會將您指向導致問題的代碼行。你可以在日誌中看到這個問題是由這個sql執行引起的:'SELECT「events」* FROM「events」WHERE「events」。「id」IN()'。這不是有效的sql - 在括號中需要有一些東西。因此,下一步是確定哪一行正在進行此調用 - 堆棧跟蹤將顯示給您。 –

+0

編輯帖子。好!我對這個術語很陌生。順便說一句堆棧跟蹤?從哪裏得到它?我剛剛發佈了更多的heroku日誌:| – nk06

+1

堆棧跟蹤是代碼處理異常的文件/行的列表 - 它通過很多層代碼「冒泡」。通常在rails中你會看到很多的rails gem引用,然後最終你的一些代碼(例如你的rails項目文件夾中的文件)。這幾乎總是這些問題中的第一個。 –

回答

1

我覺得這是你的問題:

@e_1 << @cat.events.collect{|p| p.id} if @cat.present? 
@e_2 << @dom.events.collect{|p| p.id} if @dom.present? 
@e_3 << @eli.events.collect{|p| p.id} if @eli.present? 
@e_f = @e_1 | @e_2 | @e_3 
@all = Event.where(id: @e_f) 

在這種情況下,如果@ E_1,@ E_2和@ E_3都是空白的(無),然後@e_f是零,那麼你這樣做

@all = Event.where(id: nil) 

我認爲這是什麼導致你的SQL問題。

做你想做的事情的一個好得多的辦法是這樣的:

def index 
    @cat = Category.find(params[:category][:category_id]) if params[:category][:category_id].present? 
    @dom = Domain.find(params[:domain][:domain_id]) if params[:domain][:domain_id].present? 
    @eli = Eligible.find(params[:eligible][:eligible_id]) if params[:eligible][:eligible_id].present? 
    @all = Event.find([@cat, @dom, @eli].reject(&:blank?).collect(&:event_ids).flatten.uniq) 
    @user = current_user || User.new 
end 

我認爲這可能是進一步太重構,仍然有大量的重複。

編輯:你問了最先受歡迎的排序結果。這裏有一種方法,它可能不是最有效的,幾乎肯定不是最漂亮的。它使用'按字段排序(columname,values)`,這是一種在sql中指定排序的方法。

def index 
    @cat = Category.find(params[:category][:category_id]) if params[:category][:category_id].present? 
    @dom = Domain.find(params[:domain][:domain_id]) if params[:domain][:domain_id].present? 
    @eli = Eligible.find(params[:eligible][:eligible_id]) if params[:eligible][:eligible_id].present? 
    ids = [@cat, @dom, @eli].reject(&:blank?).collect(&:event_ids).flatten 
    sorted_ids = ids.sort_by{|id| ids.select{|id2| id2 == id}.size}.reverse 
    @all = Event.find(sorted_ids, :order => "field(id, #{sorted_ids.join(",")})") 
    @user = current_user || User.new 
end 
+0

酷!有沒有辦法根據事件重複來排列最終結果?據我所知,uniq合併重複記錄。它也排序嗎? – nk06

+0

你能解釋一下你的意思嗎?請根據事件重複來理清最終結果? –

+0

a = [1,2,3,4] b = [3,4,5,7] a << b a.flatten給出了[1,2,3,4,3,4,5,7] a。 uniq給我們[1,2,3,4,5,7]。 a.flatten中有多個3和4的條目。我試圖找出如何得到結果爲[3,4,1,2,5,7]。我清楚了嗎? – nk06

0

這是錯誤:

ActionView::Template::Error (PG::Error: ERROR: syntax error at or near ")" 
SELECT "events".* FROM "events" WHERE "events"."id" IN()) 

之前我們也有類似的問題 - 它基本上是PGSQL是不能容忍MySQL或SQLite的等等。我想說解決這一問題的最好辦法就是重構你的代碼:

def index 
    cat = params[:category][:category_id] 
    domain = params[:domain][:domain_id] 
    eli = params[:eligible][:eligible_id] 

    queries = [cat, domain, eli] 
    queries.each_with_index do |query, i| 
     if query.present? 
      instance_variable_set("@#{query}", Category.find(query)) 

      e = instance_variable_get("@#{query}") 
      instance_variable_set("@e_#{i}" = e.events.collect{|p| p.id}) if defined?(e) 
     end 
    end 

    @all = Event.where(id: [@e_1, @e_2, @e_3]) 
    @user = current_user || User.new 

end 

這可能不會在第一次工作,但也有一些固定的,我敢肯定,這將解決任何你一直有

問題210