Update object identified by composite key with rails -
i have table components. each component have specific version describe above (there other attributes name, component_type_id, ...):
id | version_id
-----+------------
167 | 1
167 | 96
167 | 97
167 | 98
167 | 99
166 | 1
166 | 92
the attributes "id" , "version_id" composite primary key, , want edit object identifying composite pk.
if want update version 99 of component #167 =>
with rails when :
component.where(id: 167, version_id: 99).first.update({"component_type_id"=>"5", "name"=>"ccc"})
rails :
select "components".* "components" "components"."id" = $1 , "components"."version_id" = $2 order "components"."id" asc limit 1 [["id", 167], ["version_id", 99]] update "components" set "name" = $1, "updated_at" = $2 "components"."id" = 167 [["name", "ccc"], ["updated_at", "2016-04-19 08:05:09.049345"]]
but want :
update "components" set "name" = $1, "updated_at" = $2 "components"."id" = 167 , version_id = 99 [["name", "ccc"], ["updated_at", "2016-04-19 08:05:09.049345"]]
thanks me cheers
since these 1 record given id , version, try:
component.where(id: 167, version_id: 99).update_all(component_type_id: 5, name: "ccc")
in way, rails not instance record, avoid problem.
Comments
Post a Comment