我有模型A和模型附件。我正在编辑带有嵌套属性的A表单:attaches。当我通过accepts_nested_attributes_for从A删除所有连接时,我如何获得所有嵌套模型的after_update/after_save回调?问题是,当我在模型A中执行回调时,它们是在模型A更新之后和模型附加更新之前立即执行的,因此,例如,我不能知道在删除所有附加之后是否没有附加:)。
例如:删除所有附件后,我的回调after_save :update_status将无法正常工作。
model A
after_save :update_status
has_many :attaches
ac
我正在尝试使用dependent::destroy,但没有成功。
让我们举一个简单的例子。我使用以下内容创建了一个简单的应用程序:
rails g model parent
rails g model child parent:references
将以下行添加到parent.rb
has_many :children, dependent: :destroy
我在rails控制台(rails c)中执行以下测试。
p = Parent.create!
c = Child.create!
c.parent = p
c.save
#check association
Child.first ==
我的类如下所示,使用单表继承方法从用户继承客户。用户有属性名称和电子邮件,而订单有目的地。
class User < ActiveRecord::Base
end
class Customer < User
has_many :orders
end
class Order < ActiveRecord::Base
belongs_to :customer
end
基于以下使用Rails控制台运行的代码片段
c = Customer.create
c.orders << Order.create
c.orders.delete_all
执行最后一行中的d
我对Rails活动记录协会有点陌生。我试图建立一种关系,但是当我试图检索数据时,我会得到ActiveRecord错误。我把我的模特记错了吗?
用户有许多上传,其中有许多UserGraphs:
class User < ActiveRecord::Base
has_many :uploads, through: :user_graphs
end
class Upload < ActiveRecord::Base
has_many :users, through: :user_graphs
end
class UserGraph < ActiveRecord::Bas
我有三种型号User,Feature,UserFeature
class User < ActiveRecord::Base
has_many :user_features
has_many :features, through: :user_features
end
class Feature < ActiveRecord::Base
has_many :user_features
has_many :users, through: :user_features
end
class UserFeature < ActiveRecord::Base
我有一个rails 5项目,我从一月份开始就在做这个项目,今天我注意到我可以在不指定父记录的情况下创建子记录。这相当令人担忧,因为我验证了我使用的是rails 5.1.2和ruby 2.4.1。该网站仍在开发中。我打开了rails控制台,并测试了我的一些其他模型,它们应该有父验证(在rails 5中应该是默认的验证),这些记录也是在没有关联的情况下创建的。
这没有任何意义。我确实知道这在某种程度上是有效的。最大的变化发生在本周早些时候,我在gem中添加了用于文件上传的“回形针”。对于要检查的内容,有人有什么建议或想法吗?
class Order < ApplicationRecord
我在做一个Rails应用程序。我发现了Rails关联的一个问题:我有3种模型:用户、公司和CompanyMember
# id :integer not null, primary key
class User < ActiveRecord::Base
has_one :company_member
has_one :company, through: :company_member
end
# id :integer not null, primary key
# name :string(255) not n
说我有:
class Car < ActiveRecord::Base
belongs_to :brand
end
class Brand < ActiveRecord::Base
has_many :cars
end
如果我销毁了一个品牌对象,那么子汽车的car.brand_id属性仍然会被设置为已销毁的brand.id。
当我摧毁父母品牌的时候,我怎么能消除儿童汽车的car.brand_id呢?
我认为ActiveRecord关系可以处理这个问题,并防止孤立的对象。
我不想dependent: :destroy的汽车,但只是想让他们的car.brand_id设置为零。