Rails 关联= return 价值观和行为
Rails association= return value and behavior
该指南没有说明 return 值对于 association=
方法是什么。例如 has_one association=
对于简单的情况,它 return 是分配的对象。不过这只是赋值成功的时候。
有时 association=
会立即将更改保存在数据库中,例如设置 has_one 关联的保存记录。
association=
如何应对分配失败? (我能告诉它是否失败了吗?)
- 有没有砰!失败引发异常的版本?
association=
应该不会失败。这是对属性上的属性的简单分配。此方法不调用任何验证,并且在您调用 save
.
之前,连接不会持久保存在数据库中
赋值的 return 值是您传递给它的值。
How does association= react to assignment failure? (Can I tell if it fails?)
它不能失败。无论您分配什么,它都会按预期工作:
Behind the scenes, this means extracting the primary key from this
object and setting the associated object's foreign key to the same
value.
或者将关联保存为传入对象的字符串表示,如果对象是 "invalid"。
Is there a bang! version in which failure raises exception?
没有,没有。
因此指南的另一部分确实讨论了关联分配的 return 行为。
如果关联分配失败,则 return 为假。
这个没有bang版本
更新
周围的行为:has_many/has_one 似乎有所不同。
演示存储库:https://github.com/lulalalalistia/association-assignment-demo
在演示中,我在第一次提交时植入了一些数据,在第二次提交时出现了硬代码验证错误。演示使用 rails 4.2
has_many到
class Boss < ActiveRecord::Base
has_many :room_ownerships, as: :owner
has_many :rooms, through: :room_ownerships
end
添加房间时出现异常:
irb(main):008:0> b.rooms << Room.first
Boss Load (0.2ms) SELECT "bosses".* FROM "bosses" ORDER BY "bosses"."id" ASC LIMIT 1
Room Load (0.1ms) SELECT "rooms".* FROM "rooms" ORDER BY "rooms"."id" ASC LIMIT 1
(0.1ms) begin transaction
(0.1ms) rollback transaction
ActiveRecord::RecordInvalid: Validation failed: foo
irb(main):014:0> b.rooms
=> #<ActiveRecord::Associations::CollectionProxy []>
has_one到
class Employee < ActiveRecord::Base
has_one :room_ownership, as: :owner
has_one :room, through: :room_ownership
end
当我添加房间时,我没有得到异常:
irb(main):021:0> e.room = Room.first
Room Load (0.2ms) SELECT "rooms".* FROM "rooms" ORDER BY "rooms"."id" ASC LIMIT 1
RoomOwnership Load (0.1ms) SELECT "room_ownerships".* FROM "room_ownerships" WHERE "room_ownerships"."owner_id" = ? AND "room_ownerships"."owner_type" = ? LIMIT 1 [["owner_id", 1], ["owner_type", "Employee"]]
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> #<Room id: 1, created_at: "2016-10-03 02:32:33", updated_at: "2016-10-03 02:32:33">
irb(main):022:0> e.room
=> #<Room id: 1, created_at: "2016-10-03 02:32:33", updated_at: "2016-10-03 02:32:33">
这样就很难看出赋值是否成功了。
该指南没有说明 return 值对于 association=
方法是什么。例如 has_one association=
对于简单的情况,它 return 是分配的对象。不过这只是赋值成功的时候。
有时 association=
会立即将更改保存在数据库中,例如设置 has_one 关联的保存记录。
association=
如何应对分配失败? (我能告诉它是否失败了吗?)- 有没有砰!失败引发异常的版本?
association=
应该不会失败。这是对属性上的属性的简单分配。此方法不调用任何验证,并且在您调用 save
.
赋值的 return 值是您传递给它的值。
How does association= react to assignment failure? (Can I tell if it fails?)
它不能失败。无论您分配什么,它都会按预期工作:
Behind the scenes, this means extracting the primary key from this object and setting the associated object's foreign key to the same value.
或者将关联保存为传入对象的字符串表示,如果对象是 "invalid"。
Is there a bang! version in which failure raises exception?
没有,没有。
因此指南的另一部分确实讨论了关联分配的 return 行为。
如果关联分配失败,则 return 为假。 这个没有bang版本
更新
周围的行为:has_many/has_one 似乎有所不同。
演示存储库:https://github.com/lulalalalistia/association-assignment-demo
在演示中,我在第一次提交时植入了一些数据,在第二次提交时出现了硬代码验证错误。演示使用 rails 4.2
has_many到
class Boss < ActiveRecord::Base
has_many :room_ownerships, as: :owner
has_many :rooms, through: :room_ownerships
end
添加房间时出现异常:
irb(main):008:0> b.rooms << Room.first
Boss Load (0.2ms) SELECT "bosses".* FROM "bosses" ORDER BY "bosses"."id" ASC LIMIT 1
Room Load (0.1ms) SELECT "rooms".* FROM "rooms" ORDER BY "rooms"."id" ASC LIMIT 1
(0.1ms) begin transaction
(0.1ms) rollback transaction
ActiveRecord::RecordInvalid: Validation failed: foo
irb(main):014:0> b.rooms
=> #<ActiveRecord::Associations::CollectionProxy []>
has_one到
class Employee < ActiveRecord::Base
has_one :room_ownership, as: :owner
has_one :room, through: :room_ownership
end
当我添加房间时,我没有得到异常:
irb(main):021:0> e.room = Room.first
Room Load (0.2ms) SELECT "rooms".* FROM "rooms" ORDER BY "rooms"."id" ASC LIMIT 1
RoomOwnership Load (0.1ms) SELECT "room_ownerships".* FROM "room_ownerships" WHERE "room_ownerships"."owner_id" = ? AND "room_ownerships"."owner_type" = ? LIMIT 1 [["owner_id", 1], ["owner_type", "Employee"]]
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> #<Room id: 1, created_at: "2016-10-03 02:32:33", updated_at: "2016-10-03 02:32:33">
irb(main):022:0> e.room
=> #<Room id: 1, created_at: "2016-10-03 02:32:33", updated_at: "2016-10-03 02:32:33">
这样就很难看出赋值是否成功了。