如果您认为Rails是如何工作的,则调用只是与Call模型相关的一种方法。有几种方法可以做到这一点。一种是在关联上设置订单选项。一种是全局更改Call模型的默认范围,另一种是在Customer模型中创建一个新方法来返回调用(如果您希望在编码之前对调用进行任何操作,则很有用。)
方法1:
class Customer < ActiveRecord::base has_many :calls, :order => "created_at DESC"end
更新
对于4号及以上导轨,请使用:
class Customer < ActiveRecord::base has_many :calls, -> { order('created_at DESC') }end方法2:
class Call < ActiveRecord::base default_scope order("created_at DESC")end方法3:
class Call < ActiveRecord::base scope :recent, order("created_at DESC")endclass Customer < ActiveRecord::base def recent_calls calls.recent endend然后,您可以使用:
format.json { render :json => @customer, :methods => :recent_calls}


