新手问题 具有关联对象的 ActiveRecord 对象在创建的时候会发起相关关联表的查询

selenium · April 11, 2019 · Last by test009 replied at April 12, 2019 · 962 hits

具有关联对象的 ActiveRecord 对象在创建的时候会发起相关关联表的查询

class A < ApplicationRecord has_many :eggs end

class B < ApplicationRecord has_many :eggs end

class C < ApplicationRecord has_many :eggs end

class Egg < ApplicationRecord belongs_to :a belongs_to :b belongs_to :c end

#这条语句同时发起了对 a,b,c 表的查询,如果关联很多,那就有很多条查询语句了,我就想插入而已 Egg.create a_id: XXX, b_id: XXX,c_id: XXX

belong_to 触发存在性教研,具体去查文档

Reply to zouyu

搞定了,谢谢

怎么搞定的 说一下呀 还有比较奇怪 title_id 和 partner_id 是哪里来的 不应该是 user_id 吗

😀 感谢~

You need to Sign in before reply, if you don't have an account, please Sign up first.