新手问题 这么获得 model 的 id

xiaoxiao · June 13, 2013 · Last by xiaoxiao replied at June 13, 2013 · 2209 hits

项目用到一个供应商 model,名字叫 supplier,其中字段分别如下:company phone fax 我只知道 company 这个值 xx,怎么得到列信息的 id。 我试了一下这样不行 Supplier.where(:company=>'xx').id 会出错,错误提示是 undefined method for activerecord::relation,求助

Supplier.where(:company=>'xx').first.id

PS; 最好加上 try: Supplier.where(:company=>'xx').first.try(:id),否则容易报undefined methodid' for nil:NilClass`

You need to Sign in before reply, if you don't have an account, please Sign up first.