Here I have 2 models Branch and Subject which have many to many relationship through subjectgroup
The console shows the following result
~/workspace (master) $ rails c
Running via Spring preloader in process 4541
Loading development environment (Rails 4.2.7.1)
irb: warn: can't alias context from irb_context.
2.3.0 :001 > s=Subject.first
Subject Load (0.6ms) SELECT "subjects".* FROM "subjects" ORDER BY "subjects"."id" ASC LIMIT 1
=> #<Subject id: 1, idx_id: nil, fullname: "\tSubject1\t", created_at: "2017-05-06 23:06:32", updated_at: "2017-05-06 23:06:32">
2.3.0 :002 > s.branches.first
Branch Load (0.9ms) SELECT "branches".* FROM "branches" INNER JOIN"subjectgroups" ON "branches"."id" = "subjectgroups"."branch_id" WHERE"subjectgroups"."subject_id" = $1 ORDER BY "branches"."id" ASC LIMIT 1 [["subject_id", 1]]
=> #<Branch id: 1, name: "\Branch1\t", created_at: "2017-05-06 23:06:32", updated_at: "2017-05-06 23:06:32">
2.3.0 :003 > s.branches.find(2)
Branch Load (0.6ms) SELECT "branches".* FROM "branches" INNER JOIN "subjectgroups" ON "branches"."id" = "subjectgroups"."branch_id" WHERE "subjectgroups"."subject_id" = $1 AND "branches"."id" = $2 LIMIT 1 [["subject_id", 1], ["id", 2]]
=> #<Branch id: 2, name: "\tBranch2\t", created_at: "2017-05-06 23:06:32", updated_at: "2017-05-06 23:06:32">
2.3.0 :004 > s.branches.first.name
Branch Load (0.7ms) SELECT "branches".* FROM "branches" INNER JOIN "subjectgroups" ON "branches"."id" = "subjectgroups"."branch_id" WHERE "subjectgroups"."subject_id" = $1 ORDER BY "branches"."id" ASC LIMIT 1 [["subject_id", 1]]
=> "\tBranch1\t"
2.3.0 :005 > s.branches.find(2).name
Branch Load (0.7ms) SELECT "branches".* FROM "branches" INNER JOIN "subjectgroups" ON "branches"."id" = "subjectgroups"."branch_id" WHERE "subjectgroups"."subject_id" = $1 AND "branches"."id" = $2 LIMIT 1 [["subject_id", 1], ["id", 2]]
=> "\tBranch2\t"
2.3.0 :006 > s.branches.each do |branch|
2.3.0 :007 > branch.name
2.3.0 :008?> end
Branch Load (0.5ms) SELECT "branches".* FROM "branches" INNER JOIN "subjectgroups" ON "branches"."id" = "subjectgroups"."branch_id" WHERE "subjectgroups"."subject_id" = $1 [["subject_id", 1]]
=> [#<Branch id: 1, name: "\tBranch1\t", created_at: "2017-05-06 23:06:32", updated_at: "2017-05-06 23:06:32">, #<Branch id: 2, name: "\tBranch2\t", created_at: "2017-05-06 23:06:32", updated_at: "2017-05-06 23:06:32">]
2.3.0 :009 >
What should I do to make the result like
=> "\tBranch1\t" "\tBranch2\t"
you can try following code:
-It will print all branch names
s.branches.each do |branch|
puts branch.name
end
-It will return the names in array
s.branches.map(&:name)
Related
Can someone please explain why the following queries return diff results?
If I run a simple "last" query I get an object back.
>> j = Job.last
(1.6ms) SELECT sqlite_version(*)
Job Load (0.7ms) SELECT "jobs".* FROM "jobs" ORDER BY "jobs"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<Job id: 12, customer_id: 3, description: nil, shoot_start_date: "2020-11-25
00:00:00", shoot_end_date: "2020-11-26 00:00:00", submission_deadline: "2020-11-27
00:00:00", delivery_deadline: "2020-11-28 00:00:00", created_at: "2020-11-25 21:44:22",
updated_at: "2020-12-14 19:30:06", token: "fd2356c1-996b-4f95-b24a-8c92829af1fe", name:
"teesting">
And with this object I can get children as follows:
>> j.job_entries
JobEntry Load (0.7ms) SELECT "job_entries".* FROM "job_entries" WHERE
"job_entries"."job_id" = ? LIMIT ? [["job_id", 12], ["LIMIT", 11]]
=> #<ActiveRecord::Associations::CollectionProxy [#<JobEntry id: 8, entity_id: 12,
agency_id: 35, created_at: "2020-12-07 21:27:47", updated_at: "2020-12-07 21:27:47", job_id:
12, job_role_id: 8>, #<JobEntry id: 9, entity_id: 13, agency_id: 35, created_at: "2020-12-14
17:35:30", updated_at: "2020-12-14 17:35:30", job_id: 12, job_role_id: 7>]>
HOWEVER, when I run a method on my object which is the following code:
def self.getJobAndSubmissions(token)
Job.includes(job_entries: :images_attachments).where(:token => token)
end
I get the very same record with attachments:
>> j = Job.getJobAndSubmissions('fd2356c1-996b-4f95-b24a-8c92829af1fe')
Job Load (0.9ms) SELECT "jobs".* FROM "jobs" WHERE "jobs"."token" = ? LIMIT ? [["token",
"fd2356c1-996b-4f95-b24a-8c92829af1fe"], ["LIMIT", 11]]
JobEntry Load (0.2ms) SELECT "job_entries".* FROM "job_entries" WHERE
"job_entries"."job_id" = ? [["job_id", 12]]
ActiveStorage::Attachment Load (1.0ms) SELECT "active_storage_attachments".* FROM
"active_storage_attachments" WHERE "active_storage_attachments"."record_type" = ? AND
"active_storage_attachments"."name" = ? AND "active_storage_attachments"."record_id" IN (?,
?) [["record_type", "JobEntry"], ["name", "images"], ["record_id", 8], ["record_id", 9]]
=> #<ActiveRecord::Relation [#<Job id: 12, customer_id: 3, description: nil,
shoot_start_date: "2020-11-25 00:00:00", shoot_end_date: "2020-11-26 00:00:00",
submission_deadline: "2020-11-27 00:00:00", delivery_deadline: "2020-11-28 00:00:00",
created_at: "2020-11-25 21:44:22", updated_at: "2020-12-14 19:30:06", token: "fd2356c1-996b-
4f95-b24a-8c92829af1fe", name: "teesting">]>
But suddenly, child references no longer work.
>> j.job_entries
Traceback (most recent call last):
1: from (irb):4
Job Load (0.2ms) SELECT "jobs".* FROM "jobs" WHERE "jobs"."token" = ? LIMIT ? [["token",
"fd2356c1-996b-4f95-b24a-8c92829af1fe"], ["LIMIT", 11]]
JobEntry Load (0.1ms) SELECT "job_entries".* FROM "job_entries" WHERE
"job_entries"."job_id" = ? [["job_id", 12]]
ActiveStorage::Attachment Load (0.2ms) SELECT "active_storage_attachments".* FROM
"active_storage_attachments" WHERE "active_storage_attachments"."record_type" = ? AND
"active_storage_attachments"."name" = ? AND "active_storage_attachments"."record_id" IN (?,
?) [["record_type", "JobEntry"], ["name", "images"], ["record_id", 8], ["record_id", 9]]
NoMethodError (undefined method `job_entries' for #.
<Job::ActiveRecord_Relation:0x00005562db1258c0>)
Did you mean? entries
What the? Please help me understand why this is happening!
Job.last
will return an ActiveRecord object.
Job.includes(job_entries: :images_attachments).where(:token => token)
returns an ActiveRecord::Relation which means you can't call job_entries on it.
You can get an object with this code
# These do the same thing
Job.find_by_token(token)
Job.find_by(token: token)
NoMethodError (undefined method `job_entries' for #. <Job::ActiveRecord_Relation:0x00005562db1258c0>)
Note the error message is not for Job but for a Job::ActiveRecord_Relation.
#last returns a single Job. #where returns a set of Jobs stored in an ActiveRecord::Relation. Job::ActiveRecord_Relation is a relation specific to Job.
You can also see this in the returned value...
>> j = Job.getJobAndSubmissions('fd2356c1-996b-4f95-b24a-8c92829af1fe')
...
=> #<ActiveRecord::Relation [#<Job id: 12, ...]>
I have an instance of a model which I'm playing with in my console. The instance is called mz.
irb(main):002:0> mz.updated_at
=> Fri, 24 Apr 2020 14:01:38 UTC +00:00
irb(main):003:0> mz.created_at
=> Fri, 24 Apr 2020 14:01:38 UTC +00:00
irb(main):004:0> mz.created_at == mz.updated_at
=> false
Why is this the case?
It looks to me like it should return true.
(I am trying to solve this problem for a non abstract coding problem, but best to describe it this way I think.)
Update #1...
irb(main):003:0> m.updated_at.to_i
=> 1587741257
irb(main):004:0> m.created_at.to_i
=> 1587741257
irb(main):005:0> m.updated_at == m.created_at
=> false
Confused? I still am.
Update #2 re #DennisvandeHoef
irb(main):002:0> m.updated_at.to_i
=> 1587741257
irb(main):003:0> m.created_at.to_i
=> 1587741257
irb(main):004:0> m.updated_at = m.created_at
=> Fri, 24 Apr 2020 15:14:17 UTC +00:00
irb(main):005:0> m.save(touch: false)
(0.2ms) BEGIN
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
Tlk Load (0.4ms) SELECT "tlks".* FROM "tlks" WHERE "tlks"."id" = $1 LIMIT $2 [["id", 48], ["LIMIT", 1]]
Spkr Load (0.4ms) SELECT "spkrs".* FROM "spkrs" WHERE "spkrs"."id" = $1 LIMIT $2 [["id", 65], ["LIMIT", 1]]
ActionText::RichText Load (0.5ms) SELECT "action_text_rich_texts".* FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."record_id" = $1 AND "action_text_rich_texts"."record_type" = $2 AND "action_text_rich_texts"."name" = $3 LIMIT $4 [["record_id", 74], ["record_type", "Msg"], ["name", "safe_content"], ["LIMIT", 1]]
Msg Update (0.6ms) UPDATE "msgs" SET "updated_at" = $1 WHERE "msgs"."id" = $2 [["updated_at", "2020-04-24 15:14:17.833380"], ["id", 74]]
(6.4ms) COMMIT
=> true
irb(main):006:0> m.updated_at == m.created_at
=> true
Interesting that it works, but I have no idea why it would be different! Any thoughts?
I met a N+1 issue in this situation:
Library has many Programs. Now I want to get all the programs located a certain country, so I have a code:
country = "US"
programs = #libraries.includes(:programs).map do |library|
library.programs.where(country: country)
end
But now there is N+1 problem:
Program Load (0.8ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 15], ["country", "US"]]
Program Load (0.4ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 73], ["country", "US"]]
Program Load (0.5ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 27], ["country", "US"]]
Program Load (0.3ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 177], ["country", "US"]]
Program Load (0.3ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 38], ["country", "US"]]
Program Load (0.4ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 51], ["country", "US"]]
Program Load (0.6ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 18], ["country", "US"]]
Program Load (0.3ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 20], ["country", "US"]]
Program Load (0.5ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 42], ["country", "US"]]
Program Load (0.5ms) SELECT "programs".* FROM "programs" WHERE "programs"."library_id" = $1 AND "programs"."country" = $2 [["library_id", 39], ["country", "US"]]
Update:
My purpose is not to just filter the programs, but to use it. For example:
programs = #libraries.includes(:programs).each do |library|
if library.programs.where(country: country).size < 5
puts "US programs are less than 5 so you can still add"
end
end
Does anyone know how to solve the N+1 problem?
You can chain the where query to the includes like below
programs = #libraries.includes(:programs).where(programs: {country: country})
which should solve the N+1 problem.
See specifying-conditions-on-eager-loaded-associations
Update #1:
You can simply do it like this
programs = #libraries.includes(:programs).where(programs: {country: country}).size < 5 #returns true or false
if programs #true
puts "US programs are less than 5 so you can still add"
else #false
#your code
end
Update #2:
This should do
programs_size = #libraries.includes(:programs).where(programs: {country: country}).map { |library| library.programs.size }
Which would perform only one query and returns the size of each library.programs matching that condition as array something like below
=> [5, 4, 7, 4, 6, 2, 1]
Now you can iterate over the programs_size array and perform the logic
programs_size.each do |ps|
if ps < 5 #true
puts "US programs are less than 5 so you can still add"
else #false
#your code
end
end
I'm having an issue with the spree_I18n gem and the Spree admin panel.
i'm trying to get the admin product filter to work on spree 3.0 stable.
I found this thread : https://github.com/spree-contrib/spree_i18n/pull/602
I changed my gemfile accordingly
This is my gemfile :
gem 'spree', github: 'spree/spree', branch: '3-0-stable'
gem 'spree_gateway', github: 'spree/spree_gateway', branch: '3-0-stable'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '3-0-stable'
gem 'spree_i18n', git: 'https://github.com/mvz/spree_i18n.git', branch: '3-0-ransack-translations'
I ran bundle install and bundle update spree_i18n but The admin product filter is still returning all of the products. Is there a step that I missed ?
here are the logs :
Started GET "/admin/products?utf8=%E2%9C%93&q%5Bname_cont%5D=Mug&q%5Bvariants_including_master_sku_cont%5D=&q%5Bdeleted_at_null%5D=1" for 127.0.0.1 at 2015-08-26 13:56:39 +0200
Processing by Spree::Admin::ProductsController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>{"name_cont"=>"Mug", "variants_including_master_sku_cont"=>"", "deleted_at_null"=>"1"}}
Spree::User Load (0.2ms) SELECT "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 ORDER BY "spree_users"."id" ASC LIMIT 1 [["id", 1]]
(0.3ms) SELECT COUNT(*) FROM "spree_roles" INNER JOIN "spree_roles_users" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles_users"."user_id" = $1 AND "spree_roles"."name" = $2 [["user_id", 1], ["name", "admin"]]
(0.4ms) SELECT DISTINCT COUNT(DISTINCT "spree_products"."id") FROM "spree_products" WHERE "spree_products"."deleted_at" IS NULL
Rendered /Users/stefan/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/_index_table_options.html.erb (118.4ms)
(0.4ms) SELECT COUNT(DISTINCT count_column) FROM (SELECT DISTINCT "spree_products"."id" AS count_column FROM "spree_products" WHERE "spree_products"."deleted_at" IS NULL LIMIT 10 OFFSET 0) subquery_for_count
Spree::Product Load (0.5ms) SELECT DISTINCT "spree_products".* FROM "spree_products" WHERE "spree_products"."deleted_at" IS NULL ORDER BY "spree_products"."name" ASC LIMIT 10 OFFSET 0
Spree::Variant Load (0.2ms) SELECT "spree_variants".* FROM "spree_variants" WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."is_master" = $1 AND "spree_variants"."product_id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ORDER BY "spree_variants".position ASC [["is_master", "f"]]
Spree::Image Load (0.6ms) SELECT "spree_assets".* FROM "spree_assets" WHERE "spree_assets"."type" IN ('Spree::Image') AND "spree_assets"."viewable_type" = 'Spree::Variant' AND "spree_assets"."viewable_id" IN (17, 18, 19, 20, 21, 22, 23, 24, 25, 26) ORDER BY "spree_assets"."position" ASC
Spree::Variant Load (0.2ms) SELECT "spree_variants".* FROM "spree_variants" WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."is_master" = $1 AND "spree_variants"."product_id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) [["is_master", "t"]]
Spree::Image Load (0.4ms) SELECT "spree_assets".* FROM "spree_assets" WHERE "spree_assets"."type" IN ('Spree::Image') AND "spree_assets"."viewable_type" = 'Spree::Variant' AND "spree_assets"."viewable_id" IN (1, 2, 3, 4, 5, 7, 6, 10, 9, 8) ORDER BY "spree_assets"."position" ASC
Spree::Price Load (0.1ms) SELECT "spree_prices".* FROM "spree_prices" WHERE "spree_prices"."deleted_at" IS NULL AND "spree_prices"."currency" = $1 AND "spree_prices"."variant_id" IN (1, 2, 3, 4, 5, 7, 6, 10, 9, 8) [["currency", "USD"]]
Spree::Product::Translation Load (0.1ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 1]]
Spree::Product::Translation Load (0.2ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 2]]
Spree::Product::Translation Load (0.2ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 3]]
Spree::Product::Translation Load (0.2ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 4]]
Spree::Product::Translation Load (0.3ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 5]]
Spree::Product::Translation Load (0.2ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 6]]
Spree::Product::Translation Load (0.2ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 7]]
Spree::Product::Translation Load (0.2ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 8]]
Spree::Product::Translation Load (0.2ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 9]]
Spree::Product::Translation Load (0.2ms) SELECT "spree_product_translations".* FROM "spree_product_translations" WHERE "spree_product_translations"."spree_product_id" = $1 [["spree_product_id", 10]]
Rendered /Users/stefan/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/_index_table_options.html.erb (122.8ms)
Rendered /Users/stefan/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/products/index.html.erb within spree/layouts/admin (370.4ms)
Deface: 1 overrides found for 'spree/admin/shared/_head'
Deface: 'override' matched 1 times with 'title'
Deface: [WARNING] No :original defined for 'override', you should change its definition to include:
:original => 'b94dd9df96e085d9a869128fa811ee3aaf55fab1'
Deface: 1 overrides found for 'spree/admin/shared/_translations'
Deface: 'translation' matched 1 times with 'script'
Deface: [WARNING] No :original defined for 'translation', you should change its definition to include:
:original => '6d879f5c231ff848b4e1023dc0f8b271f922269b'
Rendered /Users/stefan/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/_translations.html.erb (27.2ms)
Rendered /Users/stefan/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/_head.html.erb (985.0ms)
Deface: 1 overrides found for 'spree/admin/shared/_header'
Deface: 'auth_admin_login_navigation_bar' matched 1 times with '[data-hook='admin_login_navigation_bar'], #admin_login_navigation_bar[data-hook]'
Deface: [ERROR] The original source for 'auth_admin_login_navigation_bar' has changed, this override should be reviewed to ensure it's still valid.
CACHE (0.0ms) SELECT COUNT(*) FROM "spree_roles" INNER JOIN "spree_roles_users" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles_users"."user_id" = $1 AND "spree_roles"."name" = $2 [["user_id", 1], ["name", "admin"]]
Rendered /Users/stefan/.rvm/gems/ruby-2.2.1/bundler/gems/spree_auth_devise-2e8e08759c4d/lib/views/backend/spree/layouts/admin/_login_nav.html.erb (1.2ms)
Rendered /Users/steven/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/_header.html.erb (15.4ms)
Rendered /Users/steven/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/sub_menu/_product.html.erb (3.3ms)
Rendered /Users/steven/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/sub_menu/_promotion.html.erb (1.2ms)
Spree::Store Load (0.4ms) SELECT "spree_stores".* FROM "spree_stores" WHERE (url like '%localhost%') ORDER BY "spree_stores"."id" ASC LIMIT 1
Spree::Store Load (0.1ms) SELECT "spree_stores".* FROM "spree_stores" WHERE "spree_stores"."default" = $1 ORDER BY "spree_stores"."id" ASC LIMIT 1 [["default", "t"]]
Spree::Country Load (0.3ms) SELECT "spree_countries".* FROM "spree_countries" WHERE "spree_countries"."id" = $1 LIMIT 1 [["id", 232]]
Rendered /Users/steven/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/sub_menu/_configuration.html.erb (7.8ms)
Rendered /Users/steven/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/_main_menu.html.erb (106.2ms)
Rendered /Users/stefan/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/_content_header.html.erb (0.2ms)
Rendered /Users/steven/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/_alert.html.erb (0.0ms)
Rendered /Users/steven/.rvm/gems/ruby-2.2.1/bundler/gems/spree-a39d06ab644e/backend/app/views/spree/admin/shared/_table_filter.html.erb (0.8ms)
Completed 200 OK in 1711ms (Views: 1697.4ms | ActiveRecord: 6.1ms)
I'm using:
ranking = Ranking.create()
ranking.send("#{month}=", rank)
ranking.save!
I'd like to append whatever value is in the #{month} column, not replace it. For example, if I am performing:
month = 'january'
ranking.send("#{month}=", 500)
ranking.save!
And then again later on:
month = 'january'
ranking.send("#{month}=", 250)
ranking.save!
The value for the column january for that particular ranking should be 750.
Is this possible with the current ActiveRecord API?
You could do this with increment! method
month = 'january'
ranking.increment!(month, 250)
updated:
to proof comments question (e.g. month = 'jan'):
irb(main):011:0> p.increment!(month, 70)
(0.0ms) begin transaction
SQL (0.0ms) UPDATE "products" SET "jan" = ?, "updated_at" = ? WHERE "product
"."id" = 1 [["jan", 171], ["updated_at", Sun, 06 Oct 2013 04:23:54 UTC +00:00]
(0.0ms) commit transaction
=> true
irb(main):012:0> p
=> #<Product id: 1, name: nil, description: nil, jan: 171, created_at: "2013-10-
06 04:22:50", updated_at: "2013-10-06 04:23:54">
and another case
irb(main):013:0> p.increment!("#{month}", 70)
(0.0ms) begin transaction
SQL (0.0ms) UPDATE "products" SET "jan" = ?, "updated_at" = ? WHERE "products
"."id" = 1 [["jan", 241], ["updated_at", Sun, 06 Oct 2013 04:24:10 UTC +00:00]]
(0.0ms) commit transaction
=> true
irb(main):014:0> p
=> #<Product id: 1, name: nil, description: nil, jan: 241, created_at: "2013-10-
06 04:22:50", updated_at: "2013-10-06 04:24:10">