【问题标题】:Rails: Undefined method error with additional ActiveRecord associationRails:带有附加 ActiveRecord 关联的未定义方法错误
【发布时间】:2015-11-18 03:59:45
【问题描述】:

我是 Rails 的新手,为了好玩而学习编码。我的目标是让老师看到所有帖子的列表,这些帖子让拥有该老师的用户。帖子属于用户,用户属于老师。

型号:

class User < ActiveRecord::Base
belongs_to :teacher
has_many :posts, dependent: :destroy

class Teacher < ActiveRecord::Base
has_many :user_posts, through: :users, source: :posts

class Post < ActiveRecord::Base
  belongs_to :user

教师控制者:

class TeachersController < ApplicationController
  before_action :find_teacher, only: [:index]

def index
 @posts = @teacher.user_posts
end

private
  def find_teacher
    @teacher = current_user
  end 
end

我正在努力处理@posts。我不断收到有关“user_posts”的未定义方法的错误。我想知道我做错了什么以及在哪里定义它。

Schema.rb(只是相关部分)

create_table "teachers", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

  add_index "teachers", ["email"], name: "index_teachers_on_email", unique: true, using: :btree
  add_index "teachers", ["reset_password_token"], name: "index_teachers_on_reset_password_token", unique: true, using: :btree

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "username"
    t.string   "TeacherEmail"
    t.string   "teacher"
  end

我昨天和今天早上一整天都在尝试解决这个问题,但我真的被困住了。提前感谢您的宝贵时间。

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    首先,如果用户belongs_to :teacher,表Users的架构应该有teacher_idinteger类型。

    t.integer "teacher_id"
    

    那么如果教师管理器有很多用户,你必须在教师模型中定义它。然后,您可以创建一个方法来查询拥有该教师的用户发布的所有帖子:

    class Teacher < ActiveRecord::Base
      has_many :users
    
      def user_posts
        Post.where(id: users.map(&:id))
      end
    end
    

    【讨论】:

    • 这与 Padmanaban Gokula 所说的相得益彰!进行所有更改后,我不得不重置数据库。谢谢大家。这对我来说是一个重要的里程碑!
    【解决方案2】:

    您在教师模型中缺少用户模型关联。

    class User < ActiveRecord::Base
      belongs_to :teacher
      has_many :posts, dependent: :destroy
    end
    
    class Teacher < ActiveRecord::Base
      has_many :users
      has_many :posts, through: :users
    end
    
    class Post < ActiveRecord::Base
      belongs_to :user
    end
    

    【讨论】:

      【解决方案3】:

      让我印象深刻的两件事:

      • users 表上没有 teacher_id 外键。 belongs_to 需要它。
      • 应该是has_many in Teacher 而不是belongs_to 用于与用户的关联。

      提示:迁移后,重新启动服务器/控制台。

      【讨论】:

        猜你喜欢
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多