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.
Related
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?
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
I have 3 courses A(july 1), B(july 2), C(july 3).A and B is rated 4 and C is rated 5.
I want to order the course like this
C should come first because it was created latest and it has higher rating than others.
A should come second because it was created first than B
I cant use order because it wont give me what i need. any way to fix this?
Here is how i am fetching the data
#courses.order('updated_at DESC, average_rating DESC')
code
[
#<Course:0x00000009f3c128
id: 6,
tutor_id: 2,
course_name: "name",
course_subtitle: "sub",
course_description: "<p>test</p> test\r\n",
video_link: "https://www.youtube.com/watch?v=UVrQcieqD0U",
course_language: "German",
course_image: "finalse.png",
created_at: Tue, 11 Jul 2017 05:03:03 UTC +00:00,
updated_at: Tue, 11 Jul 2017 08:47:03 UTC +00:00,
status: "accepted",
average_rating: 2.5,
rated_time: nil>,
#<Course:0x00000008139608
id: 7,
tutor_id: 2,
course_name: "another",
course_subtitle: "another subtuitle",
course_description: "<p>course descrition</p>\r\n",
video_link: "https://www.youtube.com/watch?v=uaTeZA-Gj7s",
course_language: "Chinese",
course_image: nil,
created_at: Tue, 11 Jul 2017 10:40:45 UTC +00:00,
updated_at: Tue, 11 Jul 2017 10:41:06 UTC +00:00,
status: "accepted",
average_rating: 2.5,
rated_time: nil>,
#<Course:0x0000000813bea8
id: 8,
tutor_id: 2,
course_name: "asfas",
course_subtitle: "were",
course_description: "<p>asdfsadf</p>\r\n",
video_link: "https://www.youtube.com/watch?v=xGytDsqkQY8",
course_language: "English",
course_image: nil,
created_at: Wed, 12 Jul 2017 03:53:26 UTC +00:00,
updated_at: Wed, 12 Jul 2017 04:32:33 UTC +00:00,
status: "accepted",
average_rating: 1.0,
rated_time: nil>,
Try:
Course.all.order("average_rating DESC, created_at ASC")
try
Course.order({ created_at: :desc, rating: :desc })
This will sort first on created_at and if two records have same created_at the will sort on the basis of rating
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)
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)