LoginSignup
1
1

More than 3 years have passed since last update.

データベースからViewに投稿者名表示させる方法いくつか(データベース検索方法)

Posted at

PicTweetという以前作ったものを下に記事をまとめます。

環境

投稿したデータでデーターベースには情報が保存されている状態です。

Viewに表示するとは?

よくよく考えて見ると

データベースの検索結果表示ということ

私の持つ教材からピックアップします。
Tweetモデルに対して「Tweet belongs to User」という形でアソシエーションを定義しているので、Tweetモデルのインスタンスが入った変数.userと記述すると、インスタンスが属しているUserモデルのインスタンスを取得できます。

(例1)アソシエーションなしの場合のコンソールを使用した取得情報

ruby
% rails c
[1] pry(main)> tweet = Tweet.find(1)
[2] pry(main)> User.find(tweet.user_id)
=> #<User id: 1, email: "test@gmail.com", encrypted_password: "@@@@@@@@@@@@@@@@@", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-12-06 09:00:00", last_sign_in_at: "2014-12-06 09:00:00", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-12-06 09:00:00", updated_at: "2014-12-06 09:00:00", nickname: "test_ruby">

(例2)アソシエーションありの場合のコンソールを使用した取得情報

ruby
% rails c
[1] pry(main)> tweet = Tweet.find(1)
[2] pry(main)> tweet.user
=> #<User id: 1, email: "test@gmail.com", encrypted_password: "@@@@@@@@@@@@@@@@@", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-12-06 09:00:00", last_sign_in_at: "2014-12-06 09:00:00", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-12-06 09:00:00", updated_at: "2014-12-06 09:00:00", nickname: "test_ruby">

私はもともとEXCEL VBAの集計を主に担当してたので、このデーターベース検索にはなレなくて
データベース検索用のメソッドfindとwhereで検索コードを4つ作りました。
(DRYという観点からはあまり良くないとは思うのですが。)

app/controllers/users_controller.rb
#①上の教材通りの解答
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @prototypes = @user.prototypes
  end
end
app/controllers/users_controller.rb
#別解②findを使って
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @prototypes = Prototype.find(user_id)
  end
end

詳細ボタンをクリックしたuser情報からfindを使ってprototypeテーブルを検索
*findメソッドに、idカラムの値を指定すると、その値を持つレコードを取り出すことができます。

参考:railsガイド「1.1 単一のオブジェクトを取り出す」

https://railsguides.jp/active_record_querying.html

app/controllers/users_controller.rb
#別解③findを使って
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @prototypes = Prototype.find(params[:id])
  end
end

②と同じ

app/controllers/users_controller.rb
#別解④whereを使って
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @prototypes = Prototype.find(params[:id])
  end
end
whereシンボル指定方法
モデル名.where(カラム名: 条件)
app/controllers/users_controller.rb
#別解⑤whereを使って
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @prototypes = Prototype.where(user_id=@user.id)
  end
end
where文字列指定方法
モデル名.where("カラム名 = 条件")

これなら今の僕の知識でもわかりやすいコードにはなっています。
さらに精進します。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1