rails mongo query based on BSON::ObjectId foreign key - ruby-on-rails

Mongodb and rails
Parent object has_many :children
Child has a parent_id of class BSON::ObjectId
Child.first.parent_id
=> BSON::ObjectId('59031cd92936094d04000d31')
I can not find the Parent with that _id
Parent.where(_id: '59031cd92936094d04000d31').first
=> nil
The Parent object looks like this
=> #<Parent _id: 5a959865c8aedf03c1000007, _type: nil, created_at: nil, updated_at: nil, id: nil, name: nil, description: nil, type: nil, starts_at: nil, children_hash: nil>

Related

neo4j uuid is not created

class Client
include Neo4j::ActiveNode
end
> client = Client.new
=> #<Client uuid: nil, bot_client_id: nil, created_at: nil, email: nil, first_name: nil, last_name: nil, sms: nil, telegram_id: nil, updated_at: nil>
My expectation is that the uuid would be populated.
The uuid is only populated once the node object has been saved. So you could either do:
client = Client.create
Or:
client = Client.new
client.save

Use string as PK and FK on Rails 4

I`m trying to use String as Primary/Foreign key on a small crawler that I'm making. But I keep receiving the following error when I try to use the Associations methods (eg.: a.crawler_details - Where a is an object called Asin):
RangeError: 8532503039 is out of range for ActiveRecord::Type::Integer with limit 4
Record example:
#<Asin asin: "8532503039", title: "O FĂ­sico", image_url: nil, active: false, created_at: "2015-05-04 03:30:29", updated_at: "2015-05-04 03:30:36">
Here are the details:
2.1.2 :001 > Asin.new
=> #<Asin asin: nil, title: nil, image_url: nil, active: true, created_at: nil, updated_at: nil>
2.1.2 :002 > CrawlerDetail.new
=> #<CrawlerDetail id: nil, amazon_price: nil, feed_price: nil, first_place: nil, second_place: nil, third_place: nil, fp_price: nil, sp_price: nil, tp_price: nil, run: nil, created_at: nil, updated_at: nil>
class Asin < ActiveRecord::Base
has_many :crawler_details, :foreign_key => 'id', :primary_key=> 'asin'
self.primary_key = 'asin'
...
end
class CrawlerDetail < ActiveRecord::Base
has_one :asin, :foreign_key => 'asin', :primary_key => 'id'
end
I also tried the belongs_to relation but with no luck. Any ideas here?
I think that is not association error , did you defined field name "asin" type integer in your database table ?
it may help please try to alter your database table from type integer to string .

Changing a user name in heroku rails console

I would like to edit a supplier name in my heroku database. I'm having trouble accessing the name attribute:
irb(main):015:0> Supplier.where(:name => "Test")
=> #<ActiveRecord::Relation [#<Supplier id: 3070, name: "Test", email: "test#me.com", phone: "555555", website: "http://www.test.co.uk", region_id: 3, category_id: 8, created_at: "2015-02-20 13:28:59", updated_at: "2015-02-20 13:28:59", rating: 0.0, address: nil, facebook_url: nil, twitter_url: nil, google_url: nil, video_url: nil, slug: "test", logo_url: nil, image_one_url: nil, image_two_url: nil, image_three_url: nil, image_four_url: nil, description: nil, reviews_count: 0, source: nil, source_other: nil>]>
irb(main):016:0> _.name
=> "Supplier"
I'm not clear why _.name is resulting in "Supplier" rather than "Test".
Can anyone tell me what I'm missing?
Supplier.where(:name => "Test") returns multiple records. Use
supplier = Supplier.where(:name => "Test").first
supplier.name

select single property in hstore field with Rails 4.1

I am using Postgres 9.3 with Rails 4.1.
Suppose I have a table with a properties hstore colum.
I now want to select a single key inside the hstore column in a scope (or somewhere else..)
What I tried:
Model.select("properties -> 'category'")
What happens: Rails gives me an array like this:
[#<Model id: nil>,
#<Model id: nil>,
#<Model id: nil>,
#<Model id: nil>]
What I want:
[#<Model id: nil, category: 'foo'>,
#<Model id: nil, category: 'bar'>,
#<Model id: nil, category: 'baz'>,
#<Model id: nil, category: 'foo'>]
Try:
=> Model.where("properties #> hstore(?, ?)", 'key', 'value').select(:category)
=> [#<Model id: nil, category: 'foo'>]

Why is my after_save callback stopping my ActiveRecord association from saving properly?

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/

Resources