ruby-on-rails - 如何让实例变量访问 rails 中的 'id'?

标签 ruby-on-rails ruby

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

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

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

我的id关系(请右击新标签打开查看更清楚):enter image description here

我的用户信息 Controller :

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

显示视频 View :

<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

用户模型:

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

显示所有已添加 ID 的我的迁移: enter image description here

我的路线:enter image description here

最佳答案

要么在用户和视频之间创建直接关联,要么通过连接模型使其间接关联。不要两者都做。

直接 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

关于ruby-on-rails - 如何让实例变量访问 rails 中的 'id'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44218743/

相关文章:

ruby - ChefSpec 不应测试包含的 Recipe

ruby - 为什么 Enumerable#to_a 在 Ruby 版本 >= 1.9.3 中以这种方式运行?

ruby-on-rails - 使用 Phusion Passenger 时出错 - 无法在任何来源中找到 sqlite3-ruby-1.2.5 (Bundler::GemNotFound)

ruby-on-rails - 使用 Devise in Rails 注销特定用户

ruby-on-rails - 如何跳过 Rails 中的所有验证?

javascript - 如何调整模态窗口的 JavaScript

涉及 utc 的 Ruby 时间操作

ruby-on-rails - 如何在同一个域上托管我的API和Web应用程序?

ruby-on-rails - 当我在 ruby​​ 中使用系统命令创建 TAR 时出现错误 "tar: Invalid transform expression"。为什么?

ruby - ruby 中突然出现 η 还原会发生什么情况?