i'm trying to get positive_power_values where it's meter_id in array
1.9.3p194 :100 > ppv = PositivePowerValue.first
PositivePowerValue _id: 518dfd25a81fc61ece8d3d23, _type: nil, deleted_at: nil, created_at: "2013-05-11 19:04:54 +0800", updated_at: nil, positive_active_energy_power_indication: "100.00", positive_reactive_power_indication: "50", use_flag: "true", meter_id: "4f71181b6e92fe054b000006"
1.9.3p194 :101 > PositivePowerValue.where(:meter_id.in => ["4f71181b6e92fe054b000006"]).count
=> 0
i wender why is 0?
i need to get positive_power_values where meter_id in array!
Are you using mongoid ?
The proper syntax would be :
PositivePowerValue.in(:meter_id => ["4f71181b6e92fe054b000006"])
Related
I have a problem with using has_many through on a concern and from a polymorphic association.
So I have 3 models and concern here:
https://gist.github.com/andreorvalho/5c2f0e3800fbb126df85
my problem is when I create a competition and do:
competition.content_permission_sources I get all the correct values, but when I do
competition. permission_sources I get an empty collection proxy, even though the content_permission_source has a permission_source:
2.1.1 :022 > c.content_permission_sources
=> #<ActiveRecord::Associations::CollectionProxy [#<ContentPermissionSource id: 1, permissible_id: 1, permissible_type: "Competition", permission_source_id: 5, is_forced: nil, created_at: "2014-05-26 19:08:40", updated_at: "2014-05-26 19:08:40">, #<ContentPermissionSource id: 2, permissible_id: 1, permissible_type: "Competition", permission_source_id: 4, is_forced: nil, created_at: "2014-05-26 19:08:40", updated_at: "2014-05-26 19:08:40">, #<ContentPermissionSource id: 3, permissible_id: 1, permissible_type: "Competition", permission_source_id: 6, is_forced: nil, created_at: "2014-05-26 19:08:40", updated_at: "2014-05-26 19:08:40">]>
2.1.1 :023 > c.permission_sources
=> #<ActiveRecord::Associations::CollectionProxy []>
2.1.1 :024 > c.content_permission_sources.first.permission_source
=> #<PermissionSource id: 5, collectable_id: 1, collectable_type: "Site", source_code: 690089, source_name: "fallback", permission_type: "newsletter", country_code: "dk", created_at: "2014-05-26 19:03:01", updated_at: "2014-05-26 19:03:01">
Anybody has an idea of what I am doing wrong and how can I make sure I can access it correctly?
Really confused here, I thought hstore stores the keys and values as strings, yet I am not getting that in console. It's storing them as a hash, take a look:
2.0.0-p247 :003 > a = Report.new
=> #<Report id: nil, name: nil, column_data: nil, user_id: nil, created_at: nil, updated_at: nil>
2.0.0-p247 :004 > a.column_data = {key: {yes: true}, value: 'hello'}
=> {:key=>{:yes=>true}, :value=>"hello"}
2.0.0-p247 :005 > a
=> #<Report id: nil, name: nil, column_data: {:key=>{:yes=>true}, :value=>"hello"}, user_id: nil, created_at: nil, updated_at: nil>
2.0.0-p247 :006 > a.column_data[:key][:yes]
=> true
2.0.0-p247 :007 > a.column_data[:key]
=> {:yes=>true}
Is something I am doing wrong or is this the new way now?
Thanks!
Here a tricky issue dealing with nested documents, references and embedded documents.
You can find a zip file containing a sample project illustrating the issue.
I'm Using
Rails v3.0.9
Mongo v1.8.3
Mongoid v2.2.0
Models
Conversation
Message
Text
Media
Link
A Conversation embeds_many Messages (autosave => true)
A Message has_many Media (autosave => true)
Text inherit from Message (STI)
Link inherit from Link (STI)
A Conversation accept_nested_attributes_for :messages
A Message accept_nested_attributes_for :medias
In rails console, let's do some testing :
ruby-1.9.2-p180 :001 > c = Conversation.new({:messages_attributes => [{'_type' => 'Text', :content => 'content', :medias_attributes => [{'_type' => 'Link', :url => 'google.com'}]}]})
=> #<Conversation _id: 4e65c7275d156d0129000001, _type: nil, created_at: nil, updated_at: nil, title: nil>
ruby-1.9.2-p180 :002 > c.messages
=> [#<Message _id: 4e65c7275d156d0129000002, _type: "Text", created_at: nil, updated_at: nil, content: "content">]
ruby-1.9.2-p180 :003 > c.messages.first.medias
=> [#<Media _id: 4e65c7275d156d0129000003, _type: "Link", created_at: nil, updated_at: nil, message_id: BSON::ObjectId('4e65c7275d156d0129000002'), url: "google.com">]
ruby-1.9.2-p180 :004 > c.save
=> true
ruby-1.9.2-p180 :005 > c.reload
=> #<Conversation _id: 4e65c7275d156d0129000001, _type: nil, created_at: 2011-09-06 07:09:33 UTC, updated_at: 2011-09-06 07:09:33 UTC, title: nil>
ruby-1.9.2-p180 :006 > c.messages
=> [#<Text _id: 4e65c7275d156d0129000002, _type: "Text", created_at: nil, updated_at: nil, content: "content">]
ruby-1.9.2-p180 :007 > c.messages.first.medias
=> [] #Should not be empty
Now if you change the relations between Conversation <=> Message as follow :
A Conversation has_many Messages
It works perfectly.
Some rails console testing also :
ruby-1.9.2-p180 :002 > c = Conversation.new({:messages_attributes => [{'_type' => 'Text', :content => 'content', :medias_attributes => [{'_type' => 'Link', :url => 'google.com'}]}]})
=> #<Conversation _id: 4e65c6bb5d156d011a000004, _type: nil, created_at: nil, updated_at: nil, title: nil>
ruby-1.9.2-p180 :003 > c.messages
=> [#<Message _id: 4e65c6bb5d156d011a000005, _type: "Text", created_at: nil, updated_at: nil, conversation_id: BSON::ObjectId('4e65c6bb5d156d011a000004'), content: "content">]
ruby-1.9.2-p180 :004 > c.messages.first.medias
=> [#<Media _id: 4e65c6bb5d156d011a000006, _type: "Link", created_at: nil, updated_at: nil, message_id: BSON::ObjectId('4e65c6bb5d156d011a000005'), url: "google.com">]
ruby-1.9.2-p180 :005 > c.save
=> true
ruby-1.9.2-p180 :006 > c.reload
=> #<Conversation _id: 4e65c6bb5d156d011a000004, _type: nil, created_at: 2011-09-06 07:07:54 UTC, updated_at: 2011-09-06 07:07:54 UTC, title: nil>
ruby-1.9.2-p180 :007 > c.messages
=> [#<Text _id: 4e65c6bb5d156d011a000005, _type: "Text", created_at: 2011-09-06 07:07:54 UTC, updated_at: 2011-09-06 07:07:54 UTC, conversation_id: BSON::ObjectId('4e65c6bb5d156d011a000004'), content: "content">]
ruby-1.9.2-p180 :008 > c.messages.first.medias
=> [#<Link _id: 4e65c6bb5d156d011a000006, _type: "Link", created_at: 2011-09-06 07:07:54 UTC, updated_at: 2011-09-06 07:07:54 UTC, message_id: BSON::ObjectId('4e65c6bb5d156d011a000005'), url: "google.com">]
Here is the link to the sample zip file : http://d.pr/oMUc
Well, not so easy on this one ^^
However here is the link to the git issue for mongoid : https://github.com/mongoid/mongoid/issues/1216
For people who have the same issue !
If you look at the four method calls below, Service.first returns a Service object, Salon.first returns a Salon object, etc. But TransactionItem.first returns a Service object. Why could this be?
ruby-1.8.7-p334 :001 > Service.first
=> #<Service id: 147, name: "Fub", salon_id: 2, created_at: "2011-08-10 18:00:07", updated_at: "2011-08-10 18:00:12", price: nil, active: true, archived: true>
ruby-1.8.7-p334 :002 > Salon.first
=> #<Salon id: 1, name: "The Cheeky Strut", created_at: nil, updated_at: nil, address_id: nil, email: nil>
ruby-1.8.7-p334 :003 > Product.first
=> #<Product id: 1, name: "Herbal Essences Shampoo", retail_price: #<BigDecimal:10305f1f0,'0.1E2',9(18)>, wholesale_price: nil, sku: "", salon_id: 2, created_at: "2011-07-08 01:35:48", updated_at: "2011-07-08 01:35:48", archived: false>
ruby-1.8.7-p334 :004 > TransactionItem.first
=> #<Service id: 63, created_at: "2011-08-30 20:05:57", updated_at: "2011-08-30 20:05:57", price: #<BigDecimal:10303eba8,'0.18E2',9(18)>>
ruby-1.8.7-p334 :005 >
This is what my app/models/transaction_item.rb looks like:
class TransactionItem < ActiveRecord::Base
belongs_to :transaction
belongs_to :stylist
end
I blew away the TransactionItem table via a migration, then created a brand new migration to re-create it. That seems to have fixed the problem.
hi i am new to ruby on rails and using mongoid with rails.
when i try to convert an mongoid object into json the attributes with nil values are skipped.
ruby-1.9.2-p180 :019 >#task
=> #<Task _id: 4e707635c7b4700ce3000004, _type: "Task", created_at: 2011-09-14 09:39:01 UTC, updated_at: 2011-09-14 09:39:01 UTC, due_date: nil, is_completed: false, assignee_id: nil, description: "hi remind this ", user_id: BSON::ObjectId('4e4d1aeac7b4700c6e000096'), item_id: BSON::ObjectId('4e53585fc7b4701082000002')>
#task.to_json(:only=>[:due_date])
=> "{}"
is there any way to get like "{\"due_date\":\"null\"}"
The easiest way to do this is to override the retrieval of due_date.
Try this in your model:
def due_date
real_value = self[:due_date]
return real_value unless real_value.nil?
return "null"
end