【问题标题】:Filtering a join table by values less than or equal to their own按小于或等于它们自己的值过滤连接表
【发布时间】:2014-10-07 19:35:38
【问题描述】:

我希望发布出勤率小于或等于 (

我的模特是……

class Student < ActiveRecord::Base

    has_many :attendances
    has_many :classes, :through => :attendances 

end

class Attendance < ActiveRecord::Base

    belongs_to :student
    validates :student_id, presence: true
    belongs_to :class
    validates :class_id, presence: true

end

class Class < ActiveRecord::Base

    has_many :attendances
    has_many :students, :through => :attendances    

end

我想向当前学生展示出勤率

所以这是在“学生”页面上,它显示了班级中其他学生的出勤率低于或等于他们自己的表格。

有人提醒我;

<% student_attend = @student.attendance.sum(:attend) %>

<% @student.joins(:attendance).where("attendance.attend <= ?", student_attend).each do |student| %>

...但这并不像我希望的那样。

有人能帮我指出正确的方向吗?

提前感谢您!如果有人可以提供帮助,很高兴向 PayPal 支付一些咖啡钱 :-)

【问题讨论】:

  • 能发一下考勤模型的结构吗?

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord ruby-on-rails-4


【解决方案1】:

您可以在控制器级别或模型级别使用以下代码片段。如果您在控制器级别使用它会更好。

您当前的学生对象是@student

@ids=[]
Student.find(:all).each do |stu|
   @ids.push stu.id if stu.attendances.count<=@student.attendances.count
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-04
    • 2017-06-16
    • 2011-01-25
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多