在我的包含公司和项目的应用程序中,这些模型的定义如下。
ActiveRecord::Schema.define(version: 20170927135313) do
create_table "companies", force: :cascade do |t|
t.string "name"
t.integer "high_price_company_id_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["high_price_company_id_id"], name: "index_companies_on_high_price_company_id_id"
end
create_table "projects", force: :cascade do |t|
t.string "name"
t.integer "price"
t.integer "company_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["company_id"], name: "index_projects_on_company_id"
end
end
high_price_company_id_id
是外键,表示高价公司记录。我打电话给save
两次。但是,这有点浪费,不是吗?如何定义有效的回拨。我只想打save
一次。
2.3.3 :001 > company = Company.new
=> #<Company id: nil, name: nil, high_price_company_id_id: nil, created_at: nil, updated_at: nil>
2.3.3 :002 > 4.times {company.projects.new(price: rand(1..100))}
=> 4
2.3.3 :003 > company.save
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "companies" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2017-09-27 15:12:54.852223"], ["updated_at", "2017-09-27 15:12:54.852223"]]
SQL (0.4ms) INSERT INTO "projects" ("price", "company_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["price", 34], ["company_id", 1], ["created_at", "2017-09-27 15:12:54.856119"], ["updated_at", "2017-09-27 15:12:54.856119"]]
SQL (0.2ms) INSERT INTO "projects" ("price", "company_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["price", 77], ["company_id", 1], ["created_at", "2017-09-27 15:12:54.859440"], ["updated_at", "2017-09-27 15:12:54.859440"]]
SQL (0.2ms) INSERT INTO "projects" ("price", "company_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["price", 50], ["company_id", 1], ["created_at", "2017-09-27 15:12:54.862167"], ["updated_at", "2017-09-27 15:12:54.862167"]]
SQL (0.3ms) INSERT INTO "projects" ("price", "company_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["price", 91], ["company_id", 1], ["created_at", "2017-09-27 15:12:54.863989"], ["updated_at", "2017-09-27 15:12:54.863989"]]
(2.8ms) commit transaction
=> true
2.3.3 :004 > max_price_proj = company.projects.maximum(:price)
(0.4ms) SELECT MAX("projects"."price") FROM "projects" WHERE "projects"."company_id" = ? [["company_id", 1]]
=> 91
2.3.3 :005 > company.high_price_company_id_id = max_price_proj
=> 91
2.3.3 :006 > company.save
(0.1ms) begin transaction
SQL (2.2ms) UPDATE "companies" SET "high_price_company_id_id" = ?, "updated_at" = ? WHERE "companies"."id" = ? [["high_price_company_id_id", 91], ["updated_at", "2017-09-27 15:13:30.411374"], ["id", 1]]
(2.7ms) commit transaction
=> true
答案 0 :(得分:0)
我认为您的FK high_price_company_id_id
的名称具有误导性。一个选项是将其重命名为highest_priced_project_id
。
要更新该FK,您可以在公司中定义回调:
before_save do
self.highest_priced_project_id = self.projects.max_by(&:price).id
end