有关Id的Sqlite问题

时间:2014-04-22 21:38:53

标签: sql ruby-on-rails ruby sqlite

我在rails上有一个Web应用程序,用户可以提交存储在sqlite DB中的数据。当提交页面显示时,它会显示数据库 ID 号码,该号码将是该提交的用户ID ,供以后参考。这是有道理的,因为它是 唯一号码

但是,我可以从10000开始而不是仅仅1来获取此号码。

=========================================

def create



@post = Post.new(post_params)
@post.save
redirect_to @post #this is the line that it errors on


end

顺便说一下我得到的错误是......

undefined method `empty?' for 100029:Fixnum

1 个答案:

答案 0 :(得分:0)

好的,在您的User类中...通过名为“big_id_s”(String)的迁移添加新列...

class User < ActiveRecord::Base
  after_create :create_big_id

  def to_param
    big_id_s
  end

  def create_big_id
    self.update_column(:big_id_s, (id + 100000).to_s)
    self
  end 
end

在您的控制器中,当您检索用户记录(显示,编辑,更新,删除)时,请使用此格式检索记录......

@user = User.find_by_big_id_s(params[:id])