【问题标题】:How do I access an associated model of an associated model?如何访问关联模型的关联模型?
【发布时间】:2013-02-11 07:21:29
【问题描述】:

我有一个包含多个帖子的主题。每个帖子都属于一个用户,每个用户都有_一个个人资料。

在特定主题的“显示”页面中,我尝试显示创建帖子的用户的个人资料信息:

<% @topic.posts.each do |post| %>

<%= post.user.profile.first_name %>

<% end %>

我收到以下错误:

nil:NilClass 的未定义方法 `profile'

知道为什么它不允许我访问个人资料吗?请指教。

我的Topic控制器如下:

class TopicsController < ApplicationController
  # GET /topics
  # GET /topics.json
  add_breadcrumb :index, :topics_path

  def index
    if params[:tag]
        @topics = Topic.tagged_with(params[:tag])
    else
        @topics = Topic.all
    end

    @newtopic = Topic.new


    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @topics }
    end
  end

  # GET /topics/1
  # GET /topics/1.json
  def show
    @topic = Topic.find(params[:id])
    @posts = @topic.posts
    @newpost = @topic.posts.build
    add_breadcrumb @topic.name

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @topic }
    end
  end

  # GET /topics/new
  # GET /topics/new.json
  def new
    add_breadcrumb :new, :topics_path
    @topic = Topic.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @topic }
    end
  end

  # GET /topics/1/edit
  def edit
    @topic = Topic.find(params[:id])
  end

  # POST /topics
  # POST /topics.json
  def create
    @topic = Topic.new(params[:topic])

    @topic.user_id = current_user.id
    @topic.last_poster_id = current_user.id
    @topic.last_post_at = Time.now


    respond_to do |format|
      if @topic.save
        format.html { redirect_to @topic, notice: 'Topic was successfully created.' }
        format.json { render json: @topic, status: :created, location: @topic }
      else
        format.html { render action: "new" }
        format.json { render json: @topic.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /topics/1
  # PUT /topics/1.json
  def update
    @topic = Topic.find(params[:id])

    respond_to do |format|
      if @topic.update_attributes(params[:topic])
        format.html { redirect_to @topic, notice: 'Topic was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @topic.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /topics/1
  # DELETE /topics/1.json
  def destroy
    @topic = Topic.find(params[:id])
    @topic.destroy

    respond_to do |format|
      format.html { redirect_to topics_url }
      format.json { head :no_content }
    end
  end
end

【问题讨论】:

  • 您有一个帖子,其中用户为 nil,这会导致错误。
  • 感谢您的回复。我查了一下,没有没有 user_id 的帖子。关于可能出了什么问题的任何其他想法?

标签: ruby-on-rails activerecord


【解决方案1】:

您的错误是由显示操作@topic.posts.build 中的这一行和视图@topic.posts.each 中的这一行引起的。由于您正在控制器中构建新帖子,@topic.posts 包含最有可能将用户设为 nil 的新记录。所以解决您的问题的方法是在您的视图中使用@posts 而不是@topic.posts

【讨论】:

    【解决方案2】:

    检查您的数据库。很可能在您的数据库中有一个不对应于任何用户的帖子。由于该帖子的用户为无,因此nil:NilClass 的配置文件未定义,即 user(null)。

    这主要发生在您创建属于用户的帖子但随后从数据库中删除属于该帖子的用户时。

    正确的方法是在你的用户模型中施加一个约束,应该是-

    class Post
     belongs_to :user, :dependent => :destroy
    end
    

    因此,如果用户被删除,该用户的相应帖子也会被删除。

    请注意,使用表强加关系后直接从数据库中删除记录不是一个好习惯。

    【讨论】:

    • 感谢您的评论。我更新了关系,以便有 :dependent => :destroy。但我也检查了所有记录,没有没有有效用户 ID 的帖子。关于它可能是什么的任何其他想法?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多