2017-05-27 5 views
1

私は "userinfos_controller"を持っています。ここでユーザーは情報を保存しています。また、プロフィールに動画をアップロードすることもできます。私は以下のようにuserinfos_controllerのshowメソッドの下でこのビデオを見せています。私は '@myvideo'にユーザーに関連付けられたビデオを取得させます。ユーザーがビデオを削除して再アップロードすると、video_idが変更されるため、video_idを使用してそのビデオを呼び出すことはできません。 .userinfo.last?Video.lastを呼びたくはありません。特定のユーザーが最後にアップロードしたビデオだけでなく、すべてのユーザーにアップロードされた最後のビデオを提供します。また、ビデオはUSER_ID、userinfo_idとVIDEO_IDを持っているので、今、私のショー法の下で、私はとき:。インスタンス変数にrailsの 'id'へのアクセスを与える方法はありますか?

それは私がそれが最後のために見てみたいが、userinfo_idに等しいVIDEO_ID探し
def show 
    @myvideo = Video.find(params[:userinfo_id]) 
end 

その特定のユーザー/ユーザー情報によってアップロードされたビデオ

My id rela tionships(右より明確に確認するためにクリックして、新しいタブで開いてください):enter image description here

マイuserinfosコントローラ:

class UserinfosController < ApplicationController 
    before_action :find_userinfo, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! 

    def index 
     @userinfors = Userinfo.all 
     @myvideo = Video.all 
    end 

    def show 
     @myvideo = Video.find(params[:userinfo_id]) 
    end 

    def new 
     @userinformation = current_user.userinfos.build 
    end 

    def create 
     @userinformation = current_user.userinfos.build(userinfo_params) 
     if @userinformation.save 
      redirect_to root_path 
     else 
      render 'new' 
     end 
    end 

    def edit 
    end 

    def update 
    end 

    def destroy 
     @userinformation.destroy 
     redirect_to userinfo_path 
    end 

    private 
     def userinfo_params 
      params.require(:userinfo).permit(:name, :email, :college, :gpa, :major) 
     end 

     def find_userinfo 
      @userinformation = Userinfo.find(params[:id]) 
     end 
end 

表示するビデオビュー:

<div> 
    <%= video_tag @myvideo.introvideo_url.to_s, :size => "480x320", :controls =>true %> 
</div> 

ビデオモデル:

class Video < ActiveRecord::Base 
    belongs_to :userinfo 
    belongs_to :user 
    mount_uploader :introvideo, VideoUploader 
end 

ユーザー情報モデル:

class Userinfo < ActiveRecord::Base 
    belongs_to :user 
    has_many :videos 
end 

Userモデル:すべてのidのが追加表示さ

class User < ActiveRecord::Base 
    has_many :vidoes 
    has_many :userinfos 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
end 

マイ移行: enter image description here

マイルート:私が正しくあなたを理解している場合enter image description here

+0

はで関係を追加します。

class User < ApplicationRecord has_many :user_infos has_many :videos, through: :user_infos # ... end class UserInfo < ApplicationRecord belongs_to :user has_many :videos end class Video < ApplicationRecord belongs_to :user_info has_one :user, through: :user_info # ... end 

どちらもあなたが行うようになります君はrモデルでは、画像は本当に理解しにくいです。 –

+0

@SebastiánPalma申し訳ありません。移行とモデルを追加しました。あなたがレールコンソールの写真をはっきりと見ることができないと話しているのであれば、右クリックして新しいタブで開きます。それは良く見えます。たくさんのことがありがとう! – Dinukaperera

+0

@SebastiánPalmaここで助けてください、バディ? https://stackoverflow.com/questions/44292947/how-to-send-user-to-their-profile-as-soon-as-they-sign-in/44293213#44293213 – Dinukaperera

答えて

1

ユーザーとビデオの間の直接の関連付けを作成するか、または結合モデルを介して間接的にする。両方をしないでください。

直接1-N:

class User < ApplicationRecord 
    has_many :videos 
    has_many :user_infos 
    # ... 
end 

class Video < ApplicationRecord 
    belongs_to :user 
    # ... 
end 

class UserInfo < ApplicationRecord 
    belongs_to :user 
    has_many :videos, through: :user 
end 

間接1-N:

@user.videos.last 
@user_info.videos.last 

def show 
    @my_video = @user_info.videos.order(created_at: :desc).last 
end 
+0

idomatially correct ruby命名は 'UserInfo'と' user_info'です。 – max

+0

両方を実行したくない理由は、外部キーを複製していることと、ActiveRecordで関連付けを使用するときに、1つの外部キーのみが入力されるためです。 – max

+0

遅れて申し訳ありませんが、私があなたの言いたことをすべて自分のコードに追加していました。詳しい説明はありがとうございます。理由は分かりませんが、2人の動画をアップロードした2人のユーザーがいます。クロームでは、1つのビデオを再生することはできません。 IEでは、どちらもうまく機能します。 lol – Dinukaperera

1

わからないが、私はあなたを考えますユーザーが以前にアップロードした動画に新しい動画をアップロードした後にアクセスしたいあなたはユーザーインスタンスを持っていないし、ちょうどUSER_IDしている場合

@user.videos.order(created_at: :desc).second 

をまたは: あなたはhas_manyの持っていると仮定:Userモデルに設定した動画の関係を、あなたはこれを行うことができます。

Video.where(userinfo_id: params[:userinfo_id]).order(created_at: :desc).second 

これが役に立ちます。

EDIT:

それともあなただけの最新のユーザーの動画にアクセスしたいです。繰り返しますが、私はあなたの関係をどのように設定するのか分かりません。私はユーザーが多くのビデオを持つことができると仮定しています。

Video.where(userinfo_id: params[:userinfo_id]).order(created_at: :desc).first 

か短い

Video.where(userinfo_id: params[:userinfo_id]).last 
+0

ちょっと仲間、ええ、それはまさに何ですか?が欲しいです。私はちょうどユーザーがアップロードした最後のビデオが欲しい。ユーザーは1つのビデオしか持てません。私はあなたの関係を見ることができるように私のモデルで質問を更新しました。 "some_id"とは何ですか? – Dinukaperera

+0

これはuser_info_idです。params [:userinfo_id]で置き換えてください。あなたはvideoinfoでビデオを検索する必要があります。最後に私は答えを更新します – blushrt

+0

助けてくれてありがとう、私はマックスの答えを出してくれました!それもチェックしてください:https://stackoverflow.com/questions/44221502/how-to -send-users-to-different-paths-in-the-input-in-rails – Dinukaperera

1

ActiveRecord#lastで試してみてください:

def show 
    @myvideo = Video.where('userinfo_id = ?', params[:userinfo_id]).last 
end 

あなたの最後のレコードを取っparams[:userinfo_id]に等しいiduserinfoにアップロードした動画を与えること。

+0

こんにちは、それは私に次のエラーを与えます: | C:/ Users/dinuka/Desktop/Railsapps/knowmenew/app/views/userinfos/show.html.erbここで、2行目が発生しました: 未定義のメソッドintro video_url for nil:NilClass | "intro_video"の出所を知ったので、私のショービューを含むように質問を更新しました。 – Dinukaperera

+0

'introvideo_url'の代わりに 'introvideo'では動作しませんか? –

+0

いいえ、そうではありません。 introvideo_urlは、私のインデックスビューでは、すべてのユーザーから使用可能なすべてのビデオを表示しており、そのビデオがそのビューに表示されるため動作します。 – Dinukaperera

関連する問題