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 !
Related
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!
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"])
When I comment out my after_save call back, my ActiveRecord associations work just fine. In Rails Console, you'd see:
> #report = Report.create :name => "foo"
=> #<Report id: 9, name: "foo", created_at: "2013-03-05 09:51:55", updated_at: "2013-03-05 09:51:55">
> #question = #report.questions.create :description => "bar"
=> #<Question id: 18, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 09:52:32", updated_at: "2013-03-05 09:52:32", additive: false, instructions: nil>
> #report.questions
=> [#<Question id: 18, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 09:52:32", updated_at: "2013-03-05 09:52:32", additive: false, instructions: nil>]
> #question.reports
=> [#<Report id: 9, name: "foo", created_at: "2013-03-05 09:51:55", updated_at: "2013-03-05 09:51:55">]
However, the associations stop working when I add the following after_save callback to question.rb:
def create_matching_surveys
self.reports.each do |report|
report.reviews.each do |review|
review.competitors.each do |competitor|
competitor.surveys.find_or_create_by_question_id(self.id)
end
end
end
end
Then, in Rails Console, you get:
> #report = Report.create :name => "foo"
=> #<Report id: 13, name: "foo", created_at: "2013-03-05 10:20:51", updated_at: "2013-03-05 10:20:51">
> #question = #report.questions.create :description => "bar"
=> #<Question id: 24, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 10:21:02", updated_at: "2013-03-05 10:21:02", additive: false, instructions: nil>
> #report.questions
=> [#<Question id: 24, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 10:21:02", updated_at: "2013-03-05 10:21:02", additive: false, instructions: nil>]
> #question.reports
=> []
This happens whether or not the report has reviews that have competitors.
The strange thing is I thought the callback was meant to happen after the question was saved? So by rights the association should save too before any of this happens, right?
How do I fix it?
UPDATE
I think I have to call the callback in the right spot in the object's life cycle, but I can't find that spot. Here's why I think this:
> #report = Report.create :name => "foo"
=> #<Report id: 20, name: "foo", created_at: "2013-03-05 12:29:35", updated_at: "2013-03-05 12:29:35">
> #question = #report.questions.create :description => "bar"
=> #<Question id: 31, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 12:30:14", updated_at: "2013-03-05 12:30:14", additive: false, instructions: nil>
> #question.reports
=> []
> #question.update_attributes :description => "foo"
=> true
> #question.reports
=> [#<Report id: 20, name: "foo", created_at: "2013-03-05 12:29:35", updated_at: "2013-03-05 12:29:35">]
BTW, the method is now in question_observer.rb:
class QuestionObserver < ActiveRecord::Observer
def after_save(model)
model.reload
model.reports.reload
model.reports.each do |report|
report.reviews.each do |review|
review.competitors.each do |competitor|
competitor.surveys.find_or_create_by_question_id(model.id)
end
end
end
return true
end
end
The answer was to use a neat new callback hook called after_commit which was introduced with Rails 3.
See http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_commit.
The only issue is after_commit doesn't work "out of the box" with transactional fixtures, but there are plenty of solutions out there, and I found this one worked well for me: https://supportbee.com/devblog/2012/01/14/testing-after_commitafter_transaction-with-rspec/
I have the following structure:
class User
include Mongoid::Document
end
class Resource
include Mongoid::Document
embeds_many :permissions
end
class Permission
include Mongoid::Document
embedded_in :resource
field :read, type: Boolean
field :write, type: Boolean
field :user_id, type: BSON::ObjectId
end
Now let's suppose I have the following data:
1.9.3p194 :001 > a = User.create
=> #<User _id: 4ff46818f83222daf9000001, _type: nil>
1.9.3p194 :002 > b = User.create
=> #<User _id: 4ff4681bf83222daf9000002, _type: nil>
1.9.3p194 :003 > r = Resource.create
=> #<Resource _id: 4ff46822f83222daf9000003, _type: nil>
1.9.3p194 :004 > r.permissions.create(read: true, user_id: a.id)
=> #<Permission _id: 4ff46835f83222daf9000004, _type: nil, read: true, write: nil, user_id: 4ff46818f83222daf9000001>
1.9.3p194 :005 > r.permissions.create(read: true, write: true, user_id: b.id)
=> #<Permission _id: 4ff4684af83222daf9000005, _type: nil, read: true, write: true, user_id: 4ff4681bf83222daf9000002>
Now I want to find all Resources for which User A has write access (which should be none)
1.9.3p194 :007 > Resource.where('permissions.write' => true).where('permissions.user_id' => a.id).all.entries
=> [#<Resource _id: 4ff46822f83222daf9000003, _type: nil>]
This obviously fails because for each where clause there is a match.
What would be the correct solution for this? Is there a way to do an and for embedded document queries?
Try this
Resource.where(:permissions.matches => {:write => true, :user_id => a.id}).all
Try this one:
Resource.Permission.where(write: true,user_id: a.id)
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.