Rails/Globalize Hide default locale from object.translations - ruby-on-rails

I'm using Globalize gem for translating DB entities. I use en locale by default in the app.
When I call podcast.translations I got:
=> [#<Podcast::Translation:0x000000000da865a8 id: 2, podcast_id: 2, locale: "en", created_at: Sat, 23 Feb 2019 05:14:44 PST -08:00, updated_at: Sat, 23 Feb 2019 05:14:44 PST -08:00, name: "test">,
#<Podcast::Translation:0x000000000da86260 id: 3, podcast_id: 2, locale: "ru", created_at: Sat, 23 Feb 2019 05:24:40 PST -08:00, updated_at: Sat, 23 Feb 2019 05:24:40 PST -08:00, name: "Русский">]
But I want to get translations without default en locale.
I know that I can use
podcast.translations.where.not(locale: :en)
But maybe exist build in solution?

Related

keep a version of an object before it's updated

I would like to keep a version of an object before it's updated.
For example:
This is the article on create:
#<Article:0x007fc32af4c910
id:1,
title: "Guitar",
price: 300,
buying_price: 50,
created_at: Sun, 17 Feb 2019 14:39:21 UTC +00:00,
updated_at: Sun, 17 Feb 2019 14:39:21 UTC +00:00,
>
This is an update of the article:
#<Article:0x007fc32af4c910
id:1,
title: "Guitar",
price: 300,
buying_price: 35,
created_at: Sun, 17 Feb 2019 14:39:21 UTC +00:00,
updated_at: Mon, 18 Feb 2019 14:39:21 UTC +00:00,
>
This the another update of the article:
#<Article:0x007fc32af4c910
id:1,
title: "Guitar",
price: 380,
buying_price: 45
created_at: Sun,17 Feb 2019 14:39:21 UTC +00:00,
updated_at: Wed, 20 Feb 2019 14:39:21 UTC +00:00,
>
See the the prices have changed, but I want to be able to report them all in an accounting table:
date | buying_price | price
17 feb |50 |300|
18 feb |35 |300|
20 feb |45 |380|
sorry I don't know how to create a table on markdown here
Accounting table is a DB table
You can use previous_changes do detect what changed and in a callback to update the accounting table.
class Article < ActiveRecord::Base
after_save :log_to_accounting_table
private
def log_to_accounting_table
p previous_changes
Accounting.create(....)
end
end
Not related to your issue: I'm using audited to track changes.
Accounting table is an Excel (what OP really wanted)
You'll need a table to store all those changes, "old versions" as you call them.
audited will help you. Then all you need to do is to export data from the audits table to a csv. Check this out to learn how to export to excel.

ActiveRecord Joins is not fetching data from multiple table. It is working for manual execution

I am building a RestAPI using Rails 5. I have 3 tables currently
Members
ContactSource (name)
ContactSourceMemberMap (member_id, contact_source_id, value)
Where,
class ContactSourceMemberMap < ApplicationRecord
belongs_to :member
belongs_to :contact_source
end
Now I want to fetch name from ContactSource and value from ContactSourceMemberMap.
I tried:
ContactSource.eager_load(:contact_source_member_maps).select("contact_sources.name", "contact_source_member_maps.value")
on ContactSourceMemberMap model.
The SQL which is getting generating is fetching data by performing INNER JOIN. But on the rails end its coming like
[#<ContactSource:0x007fa14f261950
id: 1,
name: "ContactSource1",
created_at: Thu, 08 Mar 2018 16:33:00 UTC +00:00,
updated_at: Thu, 08 Mar 2018 16:33:00 UTC +00:00>,
#<ContactSource:0x007fa14f200290
id: 2,
name: "ContactSource2",
created_at: Thu, 08 Mar 2018 16:33:00 UTC +00:00,
updated_at: Thu, 08 Mar 2018 16:33:00 UTC +00:00>,
#<ContactSource:0x007fa14fa8fb18
id: 3,
name: "ContactSource3",
created_at: Thu, 08 Mar 2018 16:33:00 UTC +00:00,
updated_at: Thu, 08 Mar 2018 16:33:00 UTC +00:00>,
#<ContactSource:0x007fa14fa8f2d0
id: 4,
name: "ContactSource4",
created_at: Thu, 08 Mar 2018 16:33:00 UTC +00:00,
updated_at: Thu, 08 Mar 2018 16:33:00 UTC +00:00>]}]
It is not displaying the value field. Any thoughts why is it behaving like that?
contact_sources = ContactSource.eager_load(:contact_source_member_maps).select("contact_sources.name", "contact_source_member_maps.value")
and then you can get the other object with
contact_sources.first.contact_source_member_maps
if I am wrong you can check the following guide

How the find the records which are not duplicates

I have a table which will have possible duplicate records.
id: 24,
name: "vamsi",
mobile: "7639817688",
company: "digi",
requirement: "mobile app",
created_at: Wed, 12 Oct 2016 11:05:33 UTC +00:00,
updated_at: Wed, 12 Oct 2016 11:05:33 UTC +00:00,
email_sent: false>,
#<Contact:0x00000006d7a4f0
id: 25,
name: "vamsi",
mobile: "7639817688",
company: "digi",
requirement: "mobile app",
created_at: Wed, 12 Oct 2016 11:05:57 UTC +00:00,
updated_at: Wed, 12 Oct 2016 11:05:57 UTC +00:00,
email_sent: false>]
Now I would like to find the unique records on which email_sent is false. I have tried this
Contact.where(email_sent: false).distinct
Contact Load (0.4ms) SELECT DISTINCT "contacts".* FROM "contacts" WHERE "contacts"."email_sent" = $1 [["email_sent", false]]
=> [#<Contact:0x00000006a1a698
id: 25,
name: "vamsi",
mobile: "7639817688",
company: "digi",
requirement: "mobile app",
created_at: Wed, 12 Oct 2016 11:05:57 UTC +00:00,
updated_at: Wed, 12 Oct 2016 11:05:57 UTC +00:00,
email_sent: false>,
#<Contact:0x00000006a1a418
id: 24,
name: "vamsi",
mobile: "7639817688",
company: "digi",
requirement: "mobile app",
created_at: Wed, 12 Oct 2016 11:05:33 UTC +00:00,
updated_at: Wed, 12 Oct 2016 11:05:33 UTC +00:00,
email_sent: false>]
But I would not want 2 records, since both are same. I would like only one to be shown. Is there any way I can solve this.
First part, the model should have validations so that these kinds of data won't be stored
It can be done via this
validates_uniqueness_of :name, scope: [:mobile, :requirement, :company]
Second part, still if you want to query something like above scenario. You have to do this
Contact.select(:name, :company, :mobile, :requirement).where(email_sent: false).distinct would be the query
ps: Answer picked up from all the comments in the question
Try group by email_sent after using where:
Contact.where(email_sent: false).group(:email_sent)

How to group date by day and sum in postgres?

I got this model:
[#<Account:0x007fcf32153098
id: 1,
profit: 100,
user_id: 1,
created_at: Sun, 15 Nov 2015 02:27:43 UTC +00:00,
updated_at: Sun, 15 Nov 2015 02:27:43 UTC +00:00>,
#<Account:0x007fcf32152df0
id: 2,
profit: 500,
user_id: 1,
created_at: Sun, 16 Nov 2015 15:05:07 UTC +00:00,
updated_at: Sun, 15 Nov 2015 15:05:07 UTC +00:00>,
]
And for now I got this to group them in date:
Account.all.group_by{|a| a.created_at.strftime("%Y-%m-%d")}
{"2015-11-15"=>
[#<Account:0x007fcf3247b1a8
id: 1,
profit: 100,
user_id: 1,
created_at: Sun, 15 Nov 2015 02:27:43 UTC +00:00,
updated_at: Sun, 15 Nov 2015 02:27:43 UTC +00:00>],
"2015-11-16"=>
[#<Account:0x007fcf3247afc8
id: 2,
profit: 500,
user_id: 1,
created_at: Sun, 16 Nov 2015 15:05:07 UTC +00:00,
updated_at: Sun, 15 Nov 2015 15:05:07 UTC +00:00>]}
My question is: How can I group them and at the same time sum the profit together if there are more than one record for that day? Seems like I can't use sum(:profit) with postgres?
I think you can just do this simply with:
Account.order("DATE(created_at)").group("DATE(created_at)").sum(:profit)

activeModel::dirty records changes even when nothing changed

I am using the ActiveModel::Dirty library. I have the following code:
def tasks_changed?
#changed = false
self.tasks.each do |task|
if task.previous_changes.any?
#changed = true
puts 'task changed so no update'
puts 'this task changed' + task.inspect.to_s
puts 'here are the changes' + task.previous_changes.to_s
end
end
return #changed
end
I a user changes something in a form, this method changes the behavior of the controller. The problem is that one of the fields is a datetime, and for some reason previous_changes thinks the datetime changes every time, regardless of whether or not it was changed.
The console is even telling me there are no changes. Here is what the puts statements above produce in the console:
task changed so no update
this task changed#<Task id: 19, title: "task 1", content: "task 2 content", created_at: "2014-03-11 17:33:26", updated_at: "2014-03-11 20:00:01", schedule_id: 1, amount: nil, time_frame: "2014-03-27 20:00:00", state: "incomplete", denied: 0, order_number: 0, finished_description: nil>
here are the changes{"time_frame"=>[Thu, 27 Mar 2014 16:00:00 EDT -04:00, Thu, 27 Mar 2014 16:00:00 EDT -04:00], "updated_at"=>[Tue, 11 Mar 2014 15:57:44 EDT -04:00, Tue, 11 Mar 2014 16:00:01 EDT -04:00]}
task changed so no update
this task changed#<Task id: 21, title: "task 2", content: "task 2 content", created_at: "2014-03-11 17:42:18", updated_at: "2014-03-11 20:00:01", schedule_id: 1, amount: nil, time_frame: "2014-03-29 20:00:00", state: "incomplete", denied: 0, order_number: 1, finished_description: nil>
here are the changes{"time_frame"=>[Sat, 29 Mar 2014 16:00:00 EDT -04:00, Sat, 29 Mar 2014 16:00:00 EDT -04:00], "updated_at"=>[Tue, 11 Mar 2014 15:57:44 EDT -04:00, Tue, 11 Mar 2014 16:00:01 EDT -04:00]}
task changed so no update
this task changed#<Task id: 22, title: "task 3 ", content: "change", created_at: "2014-03-11 18:43:23", updated_at: "2014-03-11 20:00:01", schedule_id: 1, amount: nil, time_frame: "2014-03-31 20:00:00", state: "incomplete", denied: 0, order_number: 2, finished_description: nil>
here are the changes{"time_frame"=>[Mon, 31 Mar 2014 16:00:00 EDT -04:00, Mon, 31 Mar 2014 16:00:00 EDT -04:00], "updated_at"=>[Tue, 11 Mar 2014 15:57:44 EDT -04:00, Tue, 11 Mar 2014 16:00:01 EDT -04:00]}
My basic question is why does previous_changes think that there is a change every time and how do I fix it?
At least the timeframe and the updated_at changed. Note the a time object holds more than just seconds and the inspect methods does not show milliseconds for example.
But nobody will be able to tell you why this happens without providing the code that does that.

Resources