Rails: How to update all through ActiveRecord_Relation
up vote
0
down vote
favorite
I have two related models through has_one like this:
class Asset
has_one :device
class Device
belongs_to :asset
I have an ActiveRecord_Relation of assets like this:
assets = Asset.all
I need to update a field in every Device for every asset. I do NOT want to create an array of every associated device since it will be very inefficient. I have tried things like:
assets.joins(:device).update_all( {:device=>{:my_field=>6} )
ruby-on-rails ruby-on-rails-5 rails-activerecord
add a comment |
up vote
0
down vote
favorite
I have two related models through has_one like this:
class Asset
has_one :device
class Device
belongs_to :asset
I have an ActiveRecord_Relation of assets like this:
assets = Asset.all
I need to update a field in every Device for every asset. I do NOT want to create an array of every associated device since it will be very inefficient. I have tried things like:
assets.joins(:device).update_all( {:device=>{:my_field=>6} )
ruby-on-rails ruby-on-rails-5 rails-activerecord
1
Is there a reason you can't update theDevice
directly withDevice.update_all
? Are there devices that don't belong to assets?
– Mike Gorski
Nov 8 at 18:26
The Asset.all is a simplified example. In reality, the ActiveRecord_Relation is calculated through a large series of scopes based on a wide variety of search criteria. When the search is done, I am left with 100k Asset models in an ActiveRecord_Relation.
– Elijah Hall
Nov 8 at 21:59
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have two related models through has_one like this:
class Asset
has_one :device
class Device
belongs_to :asset
I have an ActiveRecord_Relation of assets like this:
assets = Asset.all
I need to update a field in every Device for every asset. I do NOT want to create an array of every associated device since it will be very inefficient. I have tried things like:
assets.joins(:device).update_all( {:device=>{:my_field=>6} )
ruby-on-rails ruby-on-rails-5 rails-activerecord
I have two related models through has_one like this:
class Asset
has_one :device
class Device
belongs_to :asset
I have an ActiveRecord_Relation of assets like this:
assets = Asset.all
I need to update a field in every Device for every asset. I do NOT want to create an array of every associated device since it will be very inefficient. I have tried things like:
assets.joins(:device).update_all( {:device=>{:my_field=>6} )
ruby-on-rails ruby-on-rails-5 rails-activerecord
ruby-on-rails ruby-on-rails-5 rails-activerecord
asked Nov 8 at 17:53
Elijah Hall
326
326
1
Is there a reason you can't update theDevice
directly withDevice.update_all
? Are there devices that don't belong to assets?
– Mike Gorski
Nov 8 at 18:26
The Asset.all is a simplified example. In reality, the ActiveRecord_Relation is calculated through a large series of scopes based on a wide variety of search criteria. When the search is done, I am left with 100k Asset models in an ActiveRecord_Relation.
– Elijah Hall
Nov 8 at 21:59
add a comment |
1
Is there a reason you can't update theDevice
directly withDevice.update_all
? Are there devices that don't belong to assets?
– Mike Gorski
Nov 8 at 18:26
The Asset.all is a simplified example. In reality, the ActiveRecord_Relation is calculated through a large series of scopes based on a wide variety of search criteria. When the search is done, I am left with 100k Asset models in an ActiveRecord_Relation.
– Elijah Hall
Nov 8 at 21:59
1
1
Is there a reason you can't update the
Device
directly with Device.update_all
? Are there devices that don't belong to assets?– Mike Gorski
Nov 8 at 18:26
Is there a reason you can't update the
Device
directly with Device.update_all
? Are there devices that don't belong to assets?– Mike Gorski
Nov 8 at 18:26
The Asset.all is a simplified example. In reality, the ActiveRecord_Relation is calculated through a large series of scopes based on a wide variety of search criteria. When the search is done, I am left with 100k Asset models in an ActiveRecord_Relation.
– Elijah Hall
Nov 8 at 21:59
The Asset.all is a simplified example. In reality, the ActiveRecord_Relation is calculated through a large series of scopes based on a wide variety of search criteria. When the search is done, I am left with 100k Asset models in an ActiveRecord_Relation.
– Elijah Hall
Nov 8 at 21:59
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
You can use SQL:
assets.joins(:device).update_all(["devices.my_field=?", 6])
or if that field depends on others:
assets.joins(:device).update_all("devices.my_field=assets.other_field")
Your first suggestion results in: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "devices" of relation "assets" does not exist. It used this SQL: UPDATE "assets" SET devices.os_id=6 WHERE "assets"."id" IN (SELECT "assets"."id" FROM "assets" INNER JOIN "devices" ON "devices"."asset_id" = "assets"."id")
– Elijah Hall
Nov 8 at 21:58
add a comment |
up vote
0
down vote
accepted
The solution is this:
assets = Asset.some_scope.some_other_scope
devices = Device.where("devices.asset_id IN (?)", assets.select(:id))
devices.update_all("devices.my_field=?", 6)
This uses a single database query to update all associated devices through their relationship with assets.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use SQL:
assets.joins(:device).update_all(["devices.my_field=?", 6])
or if that field depends on others:
assets.joins(:device).update_all("devices.my_field=assets.other_field")
Your first suggestion results in: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "devices" of relation "assets" does not exist. It used this SQL: UPDATE "assets" SET devices.os_id=6 WHERE "assets"."id" IN (SELECT "assets"."id" FROM "assets" INNER JOIN "devices" ON "devices"."asset_id" = "assets"."id")
– Elijah Hall
Nov 8 at 21:58
add a comment |
up vote
1
down vote
You can use SQL:
assets.joins(:device).update_all(["devices.my_field=?", 6])
or if that field depends on others:
assets.joins(:device).update_all("devices.my_field=assets.other_field")
Your first suggestion results in: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "devices" of relation "assets" does not exist. It used this SQL: UPDATE "assets" SET devices.os_id=6 WHERE "assets"."id" IN (SELECT "assets"."id" FROM "assets" INNER JOIN "devices" ON "devices"."asset_id" = "assets"."id")
– Elijah Hall
Nov 8 at 21:58
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use SQL:
assets.joins(:device).update_all(["devices.my_field=?", 6])
or if that field depends on others:
assets.joins(:device).update_all("devices.my_field=assets.other_field")
You can use SQL:
assets.joins(:device).update_all(["devices.my_field=?", 6])
or if that field depends on others:
assets.joins(:device).update_all("devices.my_field=assets.other_field")
answered Nov 8 at 18:00
Vasfed
7,530102633
7,530102633
Your first suggestion results in: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "devices" of relation "assets" does not exist. It used this SQL: UPDATE "assets" SET devices.os_id=6 WHERE "assets"."id" IN (SELECT "assets"."id" FROM "assets" INNER JOIN "devices" ON "devices"."asset_id" = "assets"."id")
– Elijah Hall
Nov 8 at 21:58
add a comment |
Your first suggestion results in: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "devices" of relation "assets" does not exist. It used this SQL: UPDATE "assets" SET devices.os_id=6 WHERE "assets"."id" IN (SELECT "assets"."id" FROM "assets" INNER JOIN "devices" ON "devices"."asset_id" = "assets"."id")
– Elijah Hall
Nov 8 at 21:58
Your first suggestion results in: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "devices" of relation "assets" does not exist. It used this SQL: UPDATE "assets" SET devices.os_id=6 WHERE "assets"."id" IN (SELECT "assets"."id" FROM "assets" INNER JOIN "devices" ON "devices"."asset_id" = "assets"."id")
– Elijah Hall
Nov 8 at 21:58
Your first suggestion results in: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "devices" of relation "assets" does not exist. It used this SQL: UPDATE "assets" SET devices.os_id=6 WHERE "assets"."id" IN (SELECT "assets"."id" FROM "assets" INNER JOIN "devices" ON "devices"."asset_id" = "assets"."id")
– Elijah Hall
Nov 8 at 21:58
add a comment |
up vote
0
down vote
accepted
The solution is this:
assets = Asset.some_scope.some_other_scope
devices = Device.where("devices.asset_id IN (?)", assets.select(:id))
devices.update_all("devices.my_field=?", 6)
This uses a single database query to update all associated devices through their relationship with assets.
add a comment |
up vote
0
down vote
accepted
The solution is this:
assets = Asset.some_scope.some_other_scope
devices = Device.where("devices.asset_id IN (?)", assets.select(:id))
devices.update_all("devices.my_field=?", 6)
This uses a single database query to update all associated devices through their relationship with assets.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The solution is this:
assets = Asset.some_scope.some_other_scope
devices = Device.where("devices.asset_id IN (?)", assets.select(:id))
devices.update_all("devices.my_field=?", 6)
This uses a single database query to update all associated devices through their relationship with assets.
The solution is this:
assets = Asset.some_scope.some_other_scope
devices = Device.where("devices.asset_id IN (?)", assets.select(:id))
devices.update_all("devices.my_field=?", 6)
This uses a single database query to update all associated devices through their relationship with assets.
answered Nov 9 at 17:26
Elijah Hall
326
326
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213496%2frails-how-to-update-all-through-activerecord-relation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is there a reason you can't update the
Device
directly withDevice.update_all
? Are there devices that don't belong to assets?– Mike Gorski
Nov 8 at 18:26
The Asset.all is a simplified example. In reality, the ActiveRecord_Relation is calculated through a large series of scopes based on a wide variety of search criteria. When the search is done, I am left with 100k Asset models in an ActiveRecord_Relation.
– Elijah Hall
Nov 8 at 21:59