新手问题 这么获得 model 的 id

xiaoxiao · 2013年06月13日 · 最后由 xiaoxiao 回复于 2013年06月13日 · 2208 次阅读

项目用到一个供应商 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`

需要 登录 后方可回复, 如果你还没有账号请 注册新账号