Rails的加入产生了意外的结果

时间:2019-04-03 17:25:12

标签: ruby-on-rails postgresql activerecord ruby-on-rails-5.2

关系结构

User--> Favorite  
User --> Profile  --> Activity -->Favorite
                  --> Hobby
                  --> Address

关系

User has_one Profile
Profile has_one Activity (polymorphic association)
Profile has_one Hobby (polymorphic association )
Profile has_one Address (polymorphic association)
User has_many Activities (through Favorites )
Activity has_many Users (through Favorites )

目标

目标是列出所有显示相关“活动”,“爱好”和“地址”字段的配置文件。

因此,将显示“所有配置文件”,以及是否存在具有“ favourites.activity_id = activity.id”和“ favourites.user_id = CURRENTUSER”的“收藏夹”记录 如果该特定活动和用户没有喜欢的记录,它将显示一个标志,即1或true(如果用户已收藏该活动),以及0,nil或false(如果不存在该特定活动和用户的收藏记录)。

启用活动记录代码:

Profile.joins(:hobby,:address,:activity => :favorites).select(profiles.*,hobbies.*,addresses.*,profile.id as id, hobbies.id as hobby_id, addresses.id as address_id, favorites.*)
  

错误:代码未显示所有配置文件列表。

产生一条SQL语句或修复我的活动记录代码都将有所帮助。

架构

create_table "profiles", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
  end

  create_table "hobbies", force: :cascade do |t|

    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "hobbyable_type"
    t.bigint "hobbyable_id"
  end

  create_table "addresses", force: :cascade do |t|
    t.string "business_street"
    t.string "business_city"
    t.string "business_state"
    t.string "business_country"
    t.string "business_postal_code"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "addressable_type"
    t.bigint "addressable_id"
    t.decimal "latitude", precision: 10, scale: 6
    t.decimal "longitude", precision: 10, scale: 6

  end

  create_table "activities", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "activityable_type"
    t.bigint "activityable_id"

  end

  create_table "favorites", force: :cascade do |t|
    t.integer "user_id"
    t.integer "activity_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["activity_id"], name: "index_favorites_on_activity_id"
    t.index ["user_id", "activity_id"], name: "index_favorites_on_user_id_and_activity_id", unique: true
    t.index ["user_id"], name: "index_favorites_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.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "first_name"
    t.string "last_name"
    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.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

0 个答案:

没有答案