Ruby on rails 更改为_参数时的上一个和下一个链接

Ruby on rails 更改为_参数时的上一个和下一个链接,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我正在创建下一个和上一个链接,但遇到了一些麻烦,因为我通过修改to_param方法改变了我的url的外观: def slug name.downcase.gsub(" ", "-") end # URL will now show /id-name def to_param "#{self.id}-#{slug}" end 因此,我获取上一个链接的方法如下所示: def previous_animal self.class.first(:conditions =>

我正在创建下一个和上一个链接,但遇到了一些麻烦,因为我通过修改to_param方法改变了我的url的外观:

 def slug
  name.downcase.gsub(" ", "-")  
 end

 # URL will now show /id-name
 def to_param
  "#{self.id}-#{slug}"
 end
因此,我获取上一个链接的方法如下所示:

def previous_animal
 self.class.first(:conditions => ["id-name < ?", "what-to-put-here"], :order => "created_at desc")
end

您已经将其标记为rails 4,但似乎使用了rails 2语法

这行吗

def previous_animal
  self.class.where("id < ?", id).order(created_at: :desc).first
end
def先前的动物
self.class.where(“id<?”,id).order(在::desc创建)。第一个
结束

您已将其标记为rails 4,但似乎正在使用rails 2语法

这行吗

def previous_animal
  self.class.where("id < ?", id).order(created_at: :desc).first
end
def先前的动物
self.class.where(“id<?”,id).order(在::desc创建)。第一个
结束

您已将其标记为rails 4,但似乎正在使用rails 2语法

这行吗

def previous_animal
  self.class.where("id < ?", id).order(created_at: :desc).first
end
def先前的动物
self.class.where(“id<?”,id).order(在::desc创建)。第一个
结束

您已将其标记为rails 4,但似乎正在使用rails 2语法

这行吗

def previous_animal
  self.class.where("id < ?", id).order(created_at: :desc).first
end
def先前的动物
self.class.where(“id<?”,id).order(在::desc创建)。第一个
结束

您依赖于
id
创建时保持同步,即假设在另一个模型之后创建的模型始终具有更高的id。这通常是正确的,但取决于数据库和风险假设(例如,可以手动更改id或创建时的id)

所以我会做一些类似的事情:

def previous_animal
  animal = self.class.order('created_at desc').where('created_at < ?', self.created_at)
  animal.first if animal
end
def先前的动物
animal=self.class.order('created_at desc')。其中('created_at<?',self.created_at)
首先,如果是动物
结束

您依赖于
id
创建时保持同步,即假设在另一个模型之后创建的模型始终具有更高的id。这通常是正确的,但取决于数据库和风险假设(例如,可以手动更改id或创建时的id)

所以我会做一些类似的事情:

def previous_animal
  animal = self.class.order('created_at desc').where('created_at < ?', self.created_at)
  animal.first if animal
end
def先前的动物
animal=self.class.order('created_at desc')。其中('created_at<?',self.created_at)
首先,如果是动物
结束

您依赖于
id
创建时保持同步,即假设在另一个模型之后创建的模型始终具有更高的id。这通常是正确的,但取决于数据库和风险假设(例如,可以手动更改id或创建时的id)

所以我会做一些类似的事情:

def previous_animal
  animal = self.class.order('created_at desc').where('created_at < ?', self.created_at)
  animal.first if animal
end
def先前的动物
animal=self.class.order('created_at desc')。其中('created_at<?',self.created_at)
首先,如果是动物
结束

您依赖于
id
创建时保持同步,即假设在另一个模型之后创建的模型始终具有更高的id。这通常是正确的,但取决于数据库和风险假设(例如,可以手动更改id或创建时的id)

所以我会做一些类似的事情:

def previous_animal
  animal = self.class.order('created_at desc').where('created_at < ?', self.created_at)
  animal.first if animal
end
def先前的动物
animal=self.class.order('created_at desc')。其中('created_at<?',self.created_at)
首先,如果是动物
结束

但为什么?slug应该只在URL/路径帮助程序中生效,在数据库中查找条目应该是相同的,使用普通整数
id
。你是这样获取模型实例的。啊,好的,好的,我会更新我的问题,然后使用idI的结果。我想你会发现这是第一个需要整数的方法调用,但为什么?slug应该只在URL/路径帮助程序中生效,在数据库中查找条目应该是相同的,使用普通整数
id
。你是这样获取模型实例的。啊,好的,好的,我会更新我的问题,然后使用idI的结果。我想你会发现这是第一个需要整数的方法调用,但为什么?slug应该只在URL/路径帮助程序中生效,在数据库中查找条目应该是相同的,使用普通整数
id
。你是这样获取模型实例的。啊,好的,好的,我会更新我的问题,然后使用idI的结果。我想你会发现这是第一个需要整数的方法调用,但为什么?slug应该只在URL/路径帮助程序中生效,在数据库中查找条目应该是相同的,使用普通整数
id
。您正在以这种方式获取模型的实例。啊,好的,好的,我会更新我的问题,然后使用idI的结果。我想您会发现这是第一个需要整数
.order的方法调用(在::desc创建).first
=>
.last
?@D-side我只是用了Rich用过的东西,但我认为他是对的,因为排序。
.order(创建于::desc)。first
=>
。last
?@D-side我只是用了Rich用过的东西,但我认为他是对的,因为排序。
.order(创建于::desc).first
=>
.last
?@D-side我只是用了Rich用过的东西,但我认为他是对的,因为排序。
.order(创建于::desc)。first
=>
。last
?@D-side我只是用了Rich用过的东西,但我认为他是对的,因为排序。