2017-08-28 6 views
0

Rails 5を使用しています。私はuser_idフィールドに基づいてモデルを検索したいと思います。私のテーブルには、このRails 5では、フィールドの1つに基づいてモデルを検索するときに、未定義のfind_byメソッドを取得するのはなぜですか?

cindex=# \d user_notifications; 
           Table "public.user_notifications" 
     Column  | Type |       Modifiers 
--------------------+---------+----------------------------------------------------------------- 
id     | integer | not null default nextval('user_notifications_id_seq'::regclass) 
user_id   | integer | 
crypto_currency_id | integer | 
price    | integer | not null 
buy 

      | boolean | not null 

のように見えますが、私はそう

@user_notification = UserNotification.find_by_user(current_user) 

のようなモデルを検索しようとしたとき、私は、単純な(Railsの)何がエラーに

undefined method `find_by_user' for UserNotification:Class 

を取得見る方法私のフィールドをアップ?

答えて

1

find_by_userは、レールに組み込まれたメソッドではありません。モデルに定義する必要があります。

このIDでレコードを見つけるために使用するものは、単にFindです。入力されたidと一致するレコードがあれば、それを返します。

例:

@user_notification = UserNotifaction.find(1) 

そして、このようなカスタムIDとして、特定のフィールドで検索する、find_by

@user_notification = UserNotifaction.find_by(custom_id: 1) 
を使用
関連する問題