0
我試圖通過當前不獲取所有用戶的有效匹配:存儲計數屬性的最佳方法是什麼?
User.where("id <> ? and active_matches = 0", user.id).limit(1).order("RANDOM()")
這樣一來,我有每個用戶創建了一個比賽的時間來更新我的用戶實體的屬性active_matches。我也有一個連接表matches_users,我想知道我是否可以利用這個獲得每個用戶的活躍匹配數量?
class Match < ActiveRecord::Base
before_destroy { users.clear }
# Associations
has_and_belongs_to_many :users
belongs_to :user
class User < ActiveRecord::Base
# Associations
has_and_belongs_to_many :matches
class CreateMatches < ActiveRecord::Migration
def change
create_table :matches do |t|
t.integer :user_1
t.integer :user_2
t.boolean :active
t.integer :winner
t.timestamps
end
end
end
class CreateMatchesUsers < ActiveRecord::Migration
def change
create_table :matches_users, :id => false do |t|
t.integer :user_id
t.integer :match_id
end
end
end
create_table "users", force: true 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"
t.datetime "updated_at"
t.integer "credit", default: 0
t.integer "active_matches", default: 0
末
您能否提供有關'用戶'和'匹配'之間關係的信息,包括模型和連接表的相關模式屬性? – tompave
我的壞,更新 – manis