Rails加入用户列

时间:2016-12-26 18:46:55

标签: ruby-on-rails ruby-on-rails-4 activerecord rails-activerecord

我试图获取与同一所学校用户相关的所有活动(用户对象上的字符串),但我得到的错误是:

  

无法加入活动'到名为' users&#39 ;;也许你   拼错了吗?

activity_controller.rb

@school = current_user.school
@bathroom = Activity.includes(:users).where(name: 'Bathroom').where( :user => { :school => @school} )

和架构:

  create_table "activities", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "student_id"
    t.string   "status"
    t.integer  "user_id"
    t.index ["student_id"], name: "index_activities_on_student_id"
    t.index ["user_id"], name: "index_activities_on_user_id"
  end

  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.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "name"
    t.string   "grade"
    t.string   "school"
    t.integer  "maxout"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

1 个答案:

答案 0 :(得分:0)

不是users而是user,因为Activity属于User

@bathroom = Activity.includes(:user).where(name: 'Bathroom').where( :users => { :school => @school} )

但是User有很多activities所以User.includes(:activities)

当然,你必须提供协会:

class Activity
  belongs_to :user

class User
  has_many :activities