this is what I'm testing:
model
scope :user_pending, lambda { |user|
where("jobs.available = 0 AND jobs.user_id = ?", user.id) }
test
it "should have the scope" do
Job.should respond_to(:user_pending)
end
it "should return a list of jobs that are unavailable for the current_user" do
#user = Factory(:user, :id => 1)
#job = Factory(:job, :available => false, :user_id => #user)
Job.user_pending(#user).should == #job
end
the first test is passing, but I can't pass on second. The following error message is returning:
Failures:
1) Job Job :user_pending should return a list of jobs that are unavailable for the current_user
Failure/Error: Job.user_pending(#user).should == #job
expected: #<Job id: 6, title: "Contrata-se para fabrica de autos", location: "Aichi-ken, Toyohashi-shi", content: "Estamos contratando pessoas interessadas em trabalh...", user_id: 1, created_at: "2011-01-31 02:10:04", updated_at: "2011-01-31 02:10:04", available: false, company_name: "K.K. Test", company_website: "http://www.test.com", how_to_apply: "Enviar email para contato#test.com", locked: false, visits: 0>,
got: [#<Job id: 6, title: "Contrata-se para fabrica de autos", location: "Aichi-ken, Toyohashi-shi", content: "Estamos contratando pessoas interessadas em trabalh...", user_id: 1, created_at: "2011-01-31 02:10:04", updated_at: "2011-01-31 02:10:04", available: false, company_name: "K.K. Test", company_website: "http://www.test.com", how_to_apply: "Enviar email para contato#test.com", locked: false, visits: 0>] (using ==)
Diff:
## -1,2 +1,2 ##
-#<Job id: 6, title: "Contrata-se para fabrica de autos", location: "Aichi-ken, Toyohashi-shi", content: "Estamos contratando pessoas interessadas em trabalh...", user_id: 1, created_at: "2011-01-31 02:10:04", updated_at: "2011-01-31 02:10:04", available: false, company_name: "K.K. Test", company_website: "http://www.test.com", how_to_apply: "Enviar email para contato#test.com", locked: false, visits: 0>
+[#<Job id: 6, title: "Contrata-se para fabrica de autos", location: "Aichi-ken, Toyohashi-shi", content: "Estamos contratando pessoas interessadas em trabalh...", user_id: 1, created_at: "2011-01-31 02:10:04", updated_at: "2011-01-31 02:10:04", available: false, company_name: "K.K. Test", company_website: "http://www.test.com", how_to_apply: "Enviar email para contato#test.com", locked: false, visits: 0>]
# ./spec/models/job_spec.rb:91:in `block (3 levels) in <top (required)>'
what I missing? it's look all okay, right? I can't understand whats wrong there.
It appears that your scope is returning an array (which is what I would expect). Your test should probably look like:
Job.user_pending(#user).first.should == #job
Related
When I create a new admin user, there are three objects of translations:
Admin.last.translations.map(&:locale) # => [:ru, :uz, :uz]
Why are the locales duplicated?
Example:
admin.position_uz = 'CTO'
admin.position_ru = 'CTO'
admin.save!
admin.translations.count = 3
admin.translations.first.position = 'CTO'
admin.translations.second.position = 'Another value or empty'
admin.translations.third.position = 'CTO'
Every time the locale is set to uz, it shows the second variant.
Admin translations loo like
[
#<Admin::Translation id: 1, admin_id: 2, locale: "ru", position: "CTO", created_at: "2019-01-16 06:24:17", updated_at: "2019-01-16 06:30:31">,
#<Admin::Translation id: 2, admin_id: 2, locale: "uz", position: "Board Member", created_at: "2015-07-26 20:42:18", updated_at: "2015-07-26 20:42:18">,
#<Admin::Translation id: 3, admin_id: 2, locale: "uz", position: "CS manager", created_at: "2019-01-16 06:24:17", updated_at: "2019-01-16 07:09:21">
]
I have a model called Event, where I have stored_accessor "list" (stored like data: {"list"=>[{"key"=>"key1", "value"=>"value1"}]}).
I need to make a search query o
#<Event id: "1", title: "HHHH", description: nil, data: {"list"=>[{"key"=>"key1", "value"=>"value1"}, {"key"=>"key2", "value"=>"value2"}]}, created_at: "2017-04-14 21:06:22", updated_at: "2017-04-20 10:36:08">
#<Event id: "2", title: "HHHH", description: nil, data: {"list"=>[{"key"=>"key1", "value"=>"value1"}]}, created_at: "2017-04-14 21:06:22", updated_at: "2017-04-20 10:36:08">
#<Event id: "3", title: "HHHH", description: nil, data: {"list"=>[{"key"=>"key11", "value"=>"value11"}, {"key"=>"key12", "value"=>"value12"}]}, created_at: "2017-04-14 21:07:22", updated_at: "2017-04-20 10:37:08">
#<Event id: "4", title: "HHHH", description: nil, data: {"list"=>[{"key"=>"key111", "value"=>"value111"}, {"key"=>"key112", "value"=>"value112"}]}, created_at: "2017-04-14 21:08:22", updated_at: "2017-04-20 10:38:08">
I have a serach params like
1) {'key'=> 'key1', 'value'=> 'value1'}
2) ["key"=>"key1", "value"=>"value1"}, {"key"=>"key2", "value"=>"value2"}]
In first case, it should return Event id 1 and 2.
In second case, it should return Event id 1. (event if return 1 and 2 both could be acceptable).
I am not sure with json and array combination.
Please help.
You may do it with PostgreSQL jsonb's operator #>. Also you need to write the full path for search params: {'list' => [{'key'=> 'key1', 'value'=> 'value1'}]}. Try this code:
to_contain1 = {'list' => [{'key'=> 'key1', 'value'=> 'value1'}]}
to_contain2 = {'list' => [{'key'=> 'key2', 'value'=> 'value2'}]}
Event.
where("data #> ?", to_contain1.to_json})
# returns events 1 & 2
Event.
where("data #> ?", to_contain1.to_json).
where("data #> ?", to_contain2.to_json)
# returns event 1
I have a collection of user.paid_subscriptions in which each subscription has attributes created_at(datetime) and active(boolean).
How can I check if a PaidSubscription exists such that created_at is not equal to a certain date and active is true?
PaidSubscription looks like this:
[
#<PaidSubscription id: 11457,
user_id: 12,
period: 3,
price: 4000,
expires_at: "2016-03-08 09:44:56",
expires_at: "2016-03-08 09:44:56",
created_at: "2015-12-08 09:44:56",
updated_at: "2016-03-08 23:00:09",
active: false,
giver_id: 20573,
partial: false,
remaining_days: 0>,
#<PaidSubscription id: 13948,
user_id: 12,
period: 1,
price: 1500,
expires_at: "2016-04-11 12:07:40",
created_at: "2016-03-11 13:07:40",
updated_at: "2016-04-11 22:00:11",
active: false,
giver_id: nil,
partial: false,
remaining_days: 0>,
#<PaidSubscription id: 11458....
]
Try this,
If it is a query then it should be like this
user.paid_subscriptions.where("created_at < :date or created_at > :date and active = :active",{date: DateTime.civil(yyyy,mm,dd), active: true})
Or
if it is an array you can use it like below.
user.paid_subscriptions.any? {|ps| ps.active && ( ps.created_at.to_date < Date.civil(yyyy, mm, dd) || ps.created_at.to_date > Date.civil(yyyy, mm, dd))}
Want to get (this){example ⬇} data from db.
Have data like this :
=> #<Item id: 2330, asin: “7701C6RHIY", domain: "com”, title: “badfsd”, created_at: "2014-04-04 01:42:28">
(this) => #<Item id: 2330, asin: “7701C6RHIY", domain: "com”, title: “badfsd”, created_at: "2014-04-04 03:52:28">
=> #<Item id: 2330, asin: “7701C6RHIY", domain: "com”, title: “badfsd”, created_at: "2014-04-04 03:42:18">
=> #<Item id: 2330, asin: “7701C6RHIY", domain: "com”, title: “badfsd”, created_at: "2014-04-04 03:42:22”>
+
(this) => #<Item id: 4331, asin: “R901C6RHIY", domain: “ca”, title: “FSDs”, created_at: "2014-07-04 03:42:28">
=> #<Item id: 4331, asin: “R901C6RHIY", domain: “ca”, title: “FSDs”, created_at: "2014-04-04 03:42:28">
=> #<Item id: 4331, asin: “R901C6RHIY", domain: “ca”, title: “FSDs”, created_at: "2014-05-04 03:42:28">
+
(this) => #<Item id: 571, asin: "BSD1C6RHIY", domain: "co.jp", title: "BRIDGサイズ SCN20.A シルバー”, created_at: "2014-04-04 03:42:28">
=> #<Item id: 570, asin: "BSD1C6RHIY", domain: "co.jp", title: "BRIDGサイズ SCN20.A シルバー”, created_at: "2014-04-02 03:42:28">
=> #<Item id: 560, asin: "BSD1C6RHIY", domain: "co.jp", title: "BRIDGサイズ SCN20.A シルバー”, created_at: "2014-04-01 03:42:28">
Result something like (this) data with ids => 2330, 4331, 571
+++ UPDATE +++
Right now this is working for me:
#item_registrations = ItemRegistration.find_all_by_user_id(current_user.id)
#items = []
#item_registrations.try(:each) do |i|
#items += Item.where(:asin => i.asin, :domain => i.domain, :user_id => i .user_id).order('created_at desc').limit(1)
end
But, this stuff is slow, so, maybe, there is some other way.
+++ UPDATE2 +++
I want to do something like this:
#items = Item.where(:user_id => current_user.id).where(:created_at => ("SELECT m ax(created_at) from Items"))
getting a msg ->
PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type timestamp: "SELECT max(created_at) from Items"
LINE 1: ... "items"."user_id" = 1 AND "items"."created_at" = 'SELECT ma...
^
: SELECT "items".* FROM "items" WHERE "items"."user_id" = 1 AND "items"."created_at" = 'SELECT max(created_at) from Items' LIMIT 1
If you want to retrieve the latest created records which are uniq,you can just do this
Item.select(:id).map(&:id).order("created_at DESC").uniq
OR
Item.uniq.pluck(:id).order("created_at DESC")
which gives you the latest created records which are unique
#<Item id: 571, asin: "BSD1C6RHIY", domain: "co.jp", title: "BRIDGサイズ SCN20.A シルバー”, created_at: "2014-04-04 03:42:28">
#<Item id: 2330, asin: “7701C6RHIY", domain: "com”, title: “badfsd”, created_at: "2014-04-04 03:52:28">
#<Item id: 4331, asin: “R901C6RHIY", domain: “ca”, title: “FSDs”, created_at: "2014-07-04 03:42:28">
Note:
I'm not sure whether this is 100% correct.Please have try with it.
sql = "SELECT i.* from items i where i.created_at in (select max(i2.created_at) from items i2 group by i2.asin, i2.domain )
#items = ActiveRecord::Base.connection.execute(sql)
On rails console output what does it mean #< at the start of the hash on the following example?:
irb(main):003:0> a=Movie.all
Movie Load (0.5ms) SELECT "movies".* FROM "movies"
=> [#<Movie id: 1, title: "Aladdin", rating: "G", description: nil, release_date: "1992-11-25 00:00:00", created_at: "2013-07-27 21:29:01", updated_at: "2013-07-27 21:29:01">, # <Movie id: 2, title: "The Terminator", rating: "R", description: nil, release_date: "1984-10- 26 00:00:00", created_at: "2013-07-27 21:29:01", updated_at: "2013-07-27 21:29:01">, #<Movie id: 3, title: "When Harry Met Sally", rating: "R", description: nil, release_date: "1989-07-21 00:00:00", created_at: "2013-07-27 21:29:01", updated_at: "2013-07-27 21:29:01">,... more output
That's how an object is printed in ruby, for example and instance of class Movie would be printed something like this:
<#Movie:0x003247fa... >
| |
class memory position I think
What you have there is a set of this previous writing:
[ one_object, other_object ... ]
To have a better display you could use hirb.
#< means that this is an instance of the Movie class.