简体   繁体   English

Rails 在与 datetime 相同的数据类型“period_end”上对多个模型进行排序

[英]Rails sorting multiple models on the same datatype "period_end" as datetime

So far I have this for the sorting到目前为止,我有这个用于排序

sets = Array.new
sets += current_user.experiences.period_end + current_user.educations.period_end
sets.sort! do |current_user.experiences, current_user.educations| 
  current_user.experiences.period_end <==> current_user.educations.period_end
end

with this for the models与这个模型

class Experience < ApplicationRecord
    attr_accessible :period_end

...
class EducationExperience < ApplicationRecord
    attr_accessible :period_end
...

And in the display on HTML I'd like to make an if statement to see if it's an experience or an education experience to change the color of the text.在 HTML 上的显示中,我想做一个 if 语句,看看改变文本颜色是一种体验还是一种教育体验。

You don't need to make if statements, just set proper class name to each container respectively.你不需要做 if 语句,只需为每个容器分别设置适当的类名。 For example (using Slim):例如(使用 Slim):

div(class="#{model_name.class.to_s.underscore}")
 = model_name.desired_property

Your div will have class experience or education_experience respectively and you can adjust styles for them.您的 div 将分别具有 class experienceeducation_experience ,您可以为它们调整样式。 Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM