如何为实例变量提供对“id”的访问权限。在铁路?

时间:2017-05-27 15:41:23

标签: ruby-on-rails ruby

我有一个" userinfos_controller"用户存储其信息的位置。他们还可以将视频上传到他们的个人资料中。我在userinfos_controller中的show方法下显示此视频,如下所示。我想要' @ myvideo"获取与用户关联的视频。当用户删除其视频并重新上传时,video_id会发生变化。所以我无法使用video_id调用该视频。有没有办法调用Video.userinfo.last?我不想打电话给Video.last,因为它会向我提供所有用户上传的最后一个视频,而不仅仅是该特定用户上传的最后一个视频。您还可以在我的rails控制台中看到视频有user_id,userinfo_id和video_id。所以现在,根据我的show方法,当我这样做时:

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

它查找的Video_id等于userinfo_id,而我希望它查找该特定用户/ userinfo上传的最后一个视频。

我的ID关系(请右键单击并在新标签页中打开以更清楚地看到):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

Userinfo模型:

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

用户模型:

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

3 个答案:

答案 0 :(得分:1)

不确定我是否理解正确,但我认为您希望在上传新视频后访问用户之前上传的视频。 假设您在User模型上设置了has_many:videos关系,则可以执行以下操作:

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

或者,如果您没有用户实例并且只有user_id。

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

希望这会有所帮助。

编辑:

或许您只想访问最新的用户视频。再说一遍,我不知道你是如何建立关系的。我假设用户可以有很多视频。

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

或更短

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

答案 1 :(得分:1)

尝试ActiveRecord#last

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

这将为您提供上传到userinfo的视频,其中id等于params[:userinfo_id]获取最后一条记录。

答案 2 :(得分: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:

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

两者都可以让你这样做:

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