使用rails中的子项订购父模型

时间:2015-06-23 12:22:05

标签: ruby-on-rails models

我有型号productvote,我需要按照票数DESC订购产品。

产品has_many :votes,投票belongs_to :product

index_controller.rb

def index
  @products = Product.all
end

2 个答案:

答案 0 :(得分:0)

Select * from Employee as E
  Join EmployeeDate as ED 
    on E.EmployeeID = ED.EmployeeID
  Join DateTable D 
    on D.Dateid = ED.Dateid
Where D.fulldate = <your full Date>

如果您还没有投票栏,请使用:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)

t = np.arange(0.0, 100.0, 0.1)
s = np.sin(2*np.pi*t)
l, = plt.plot(t,s)
plt.axis([0, 10, -1, 1])

axcolor = 'lightgoldenrodyellow'
axpos = plt.axes([0.2, 0.1, 0.65, 0.03], axisbg=axcolor)

spos = Slider(axpos, 'Pos', 0.1, 90.0)

def update(val):
    pos = spos.val
    ax.axis([pos,pos+10,-1,1])
    fig.canvas.draw_idle()

spos.on_changed(update)

plt.show()

答案 1 :(得分:0)

@products = Product.joins(:votes).order(&#34; votes.id&#34;)