mongoid query rails 3 - ruby-on-rails-3

brand new to MongoDB / mongoid and still pretty green on ruby / rails
on the rails console:
t = Task.all(:conditions => {:title => "task2"})
=> #<Mongoid::Criteria
selector: {:title=>"task2"},
options: {},
class: Task,
embedded: false>
>> t.first
=> #<Task _id: 4d7e2cdb73ec3227dd000009, title: "task2", latitude: 23.987904829, longitude: -12.9423875, created_by: "4d792d6973ec3248e3000001">
returns what I would expect, but
t = Task.where(:created_by => "4d792d6973ec3248e3000001")
=> #<Mongoid::Criteria
selector: {:created_by=>BSON::ObjectId('4d792d6973ec3248e3000001')},
options: {},
class: Task,
embedded: false>
>> t.first
=> nil
same result with:
t = Task.all(:conditions => {:created_by => "4d792d6973ec3248e3000001"})
here is the model:
class Task
include Mongoid::Document
field :title
field :latitude, :type => Float
field :longitude, :type => Float
field :created_by
end
what am I doing wrong ?
updated example:
>> Task.new(:title => "task8", :created_by => "4d792d6973ec3248e3000001")
=> #<Task _id: 4d81037973ec32cc22000003, latitude: nil, longitude: nil, title: "task8", created_by: "4d792d6973ec3248e3000001">
>> t = Task.all(:conditions => {:created_by => "4d792d6973ec3248e3000001"})
=> #<Mongoid::Criteria
selector: {:created_by=>BSON::ObjectId('4d792d6973ec3248e3000001')},
options: {},
class: Task,
embedded: false>
>> t.first
=> nil
do I need to specifically define created_by as :type => String ?

Looks like created_by is stored as a string in your document, but then you query looking for an ObjectId. ObjectId's and strings aren't the same.
Try changing
>> Task.new(:title => "task8", :created_by => "4d792d6973ec3248e3000001")
to
>> Task.new(:title => "task8", :created_by => BSON::ObjectId("4d792d6973ec3248e3000001"))

You have to include this functionality in mongoid as explained in docs:
class Person
include Mongoid::Document
include Mongoid::Timestamps
end

Related

Elixir: extract element in a large structured data

I made a get request with HTTPoison, but now I'm trying to parse the JSON file. For that I'm using Poison library. But I'm having some problems to get an element from the JSON object.
Here my code :
def parse_json do
IO.puts("\nLet's parse JSON file.")
url = "https://entreprise.data.gouv.fr/api/sirene/v3/etablissements/?page=1&per_page=1&etat_administratif=A&denomination_usuelle=" <> "SAFENERGY"
start()
case get(url) do
{:ok, %{status_code: 200, body: body}} ->
IO.puts("Success research.")
decode!(body)
#|> Map.to_list() NON-FUNCTIONAL
#|> Enum.find(&match?(["etablissements" | _], &1)) NON-FUNCTIONAL
#|> Enum.find_value(fn %{"siren" => siren} -> siren end) NON-FUNCTIONAL
#|> IO.puts(&(&1.siren)) NON-FUNCTIONAL
|> IO.inspect()
{:ok, %{status_code: 404}} ->
IO.puts("None match between establishment's name and Sirene API.")
{:ok, %{status_code: 500}} ->
IO.puts("Nonfunctional Sirene API's server (maintenance...).")
{:ok, %{status_code: 429}} ->
IO.puts("Exceeding the maximum call volume (7 requests/s maximum).")
{:error, %{reason: reason}} ->
IO.puts("Failure research.")
IO.inspect(reason)
_ -> IO.puts("Unknown error! Good luck to find it!")
end
end
My output:
%{
"etablissements" => [
%{
"statut_diffusion" => "O",
"libelle_voie" => "MAS DES PERES",
"distribution_speciale_2" => nil,
"geo_ligne" => "G",
"libelle_voie_2" => nil,
"unite_legale_id" => 33014385,
"type_voie_2" => nil,
"geo_type" => "street",
"code_postal_2" => nil,
"libelle_pays_etranger" => nil,
"indice_repetition_2" => nil,
"libelle_commune" => "MAUGUIO",
"siret" => "48944519700036",
"id" => 70725092,
"distribution_speciale" => nil,
"indice_repetition" => nil,
"siren" => "489445197",
"complement_adresse" => nil,
"unite_legale" => %{
"statut_diffusion" => "O",
"nic_siege" => "00036",
"prenom_usuel" => nil,
"sigle" => nil,
"denomination" => "SAFENERGY",
"id" => 33014385,
"pseudonyme" => nil,
"nom_usage" => nil,
"siren" => "489445197",
"date_dernier_traitement" => "2019-11-13T15:06:19",
"annee_effectifs" => nil,
"categorie_entreprise" => "PME",
"tranche_effectifs" => nil,
"identifiant_association" => nil,
"sexe" => nil,
"prenom_2" => nil,
"caractere_employeur" => "N",
"nom" => nil,
"created_at" => "2020-07-02T02:56:19.773+02:00",
"economie_sociale_solidaire" => "N",
"prenom_4" => nil,
"date_fin" => nil,
"date_debut" => "2019-07-01",
"prenom_3" => nil,
"date_creation" => "2006-04-01",
"annee_categorie_entreprise" => "2017",
"denomination_usuelle_2" => nil,
"denomination_usuelle_3" => nil,
"denomination_usuelle_1" => nil,
...
},
"date_dernier_traitement" => "2019-11-13T15:06:19",
"annee_effectifs" => nil,
"denomination_usuelle" => "SAFENERGY",
"code_pays_etranger" => nil,
"complement_adresse_2" => nil,
"tranche_effectifs" => nil,
"enseigne_1" => nil,
"numero_voie_2" => nil,
"geo_id" => "34154_b163",
"activite_principale_registre_metiers" => nil,
"geo_l5" => nil,
"caractere_employeur" => "N",
"geo_l4" => "MAS DES PERES",
"nic" => "00036",
"code_postal" => "34130",
"libelle_cedex_2" => nil,
"created_at" => "2020-07-02T03:12:45.814+02:00",
"numero_voie" => "9002",
"longitude" => "3.967449",
"type_voie" => nil,
"date_debut" => "2019-07-01",
"code_cedex_2" => nil,
"date_creation" => "2019-07-01",
"code_commune_2" => nil,
"libelle_pays_etranger_2" => nil,
"libelle_commune_etranger" => nil,
"latitude" => "43.603798",
"code_cedex" => nil,
"geo_score" => "0.95",
...
}
],
"meta" => %{
"page" => 1,
"per_page" => 1,
"total_pages" => 1,
"total_results" => 1
}
}
Like you can see above (four lines in comments), I'm trying to extract "siren" element for instance but it failed... Am I off the mark ?
Use Access in general and Kernel.get_in/2 here in particular
body
|> decode!()
|> get_in(["etablissements", Access.all(), "unite_legale", "siren"])
#⇒ "0"
With Access you might filter the outcome on each step, get all branches, etc. Here we use Access.all/0 to get to all elements of the list.
I guess you want to iterate over all "etablissements" and retrieve all SIREN numbers. You can try something like:
body
|> decode!()
|> Map.get("etablissements")
|> Enum.map(fn etablissement -> Map.get(etablissement, "siren") end)
assuming the data structure is the one shown above. Notice that Enum.map/2 is not related to the Map module, but runs the function fn etablissement -> Map.get(etablissement, "siren") end on each entry of the "etablissements" list.
This should return a list with all SIREN numbers. If there might be duplicate entries, you can use Enum.uniq/1.

Silex and doctrine orm mapping - no mappings detected

I have a problem with setting up entities mapping in silex app.
Registering services:
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_mysql',
'dbname' => 'blabla',
'host' => 'blabla',
'user' => 'blabla',
'password' => 'blabla',
'port' => '3306',
'charset' => 'utf8'
),
));
$app->register(new DoctrineOrmServiceProvider(), [
'orm.em.options.mappings' => [
[
'type' => 'simple_yml',
'namespace' => 'App\Entities',
'path' => __DIR__. '/Resources/orm/mappings/',
],
]
]);
src/Resources/orm/mappings/User.orm.yml:
User:
type: entity
table: users
id:
id:
type: integer
generator:
strategy: AUTO
fields:
username:
type: string
email:
type: string
github_token:
type: string
github_token_created_at:
type: datetimetz
created_at:
type: datetimetz
updated_at:
type: datetimetz
And entity class itself is in: src/Entities/User.php
cli-config.php file:
<?php
require_once 'vendor/autoload.php';
$app = require_once 'src/app.php';
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($app['orm.em']);
And I am running following command:
vendor/bin/doctrine orm:info
I am getting info about lack of mappings:
[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors`
I am 99% sure paths are set up ok, the connection to database is correct, I have no idea why it doesn't work. Could someone help me with this?
Most mappings i've seen are like this
'mappings' => array(
// Using actual filesystem paths
array(
'type' => 'annotation',
'namespace' => 'Foo\Entities',
'path' => __DIR__.'/src/Foo/Entities',
),
You do not seem to have your namespace and path in any notable relationship

elasticsearch geo_distance query give me error how to write geo_distance query?

I have a table, schools with the fields: id, name, address, city, state, zip, latitude and longitude.
I want to search for schools within 12km by giving latitude and longitude; I am using this query but it's not working.
curl -X GET "http://localhost:9200/schools/school/_search?=true" -d '{"filter" : {"geo_distance" : {"distance" :"12km", "location": "40,-70"}}}'
I get the following error:
{
"error":"SearchPhaseExecutionException[
Failed to execute phase [query], total failure;
shardFailures {[_na_][schools][0]: No active shards}{[_na_][schools][1]:
No active shards}{[_na_][schools][2]: No active shards}{[_na_][schools][4]:
No active shards}{[WJV55VsxQU-XW8VPmXImwA][schools][3]:
RemoteTransportException[[Archie Corrigan][inet[/192.168.1.109:9300]][search/phase/query]]; nested:
SearchParseException[[schools][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [
{\"filter\" :
{\"geo_distance\" :
{\"distance\" :\"12km\", \"location\": \"40,-70\"}
}
}]
]
]; nested: QueryParsingException[[schools] failed to find geo_point field [location]];}]",
"status":500
}
model configuration in rails
# ElasticSearch integration
include Tire::Model::Callbacks
include Tire::Model::Search
include Search::ReloadHelper
tire do
settings({
:analysis => {
:filter => Search::Filters.hs_name_filters,
:analyzer => Search::Analyzers.hs_name_analyzers
}
})
mapping do
indexes :id, :type => 'integer', :index => :not_analyzed
indexes :name, :type => 'string', :analyzer => :hs_name_analyzer
indexes :address, :type => 'string', :index => :not_analyzed
indexes :city, :type => 'string', :analyzer => :hs_name_analyzer
indexes :state, :type => 'string', :index => :not_analyzed
indexes :zip, :type => 'integer', :index => :not_analyzed
indexes :location, :type => 'geo_point', :lat_lon => true
end
end
def to_indexed_json
{
:id => id,
:name => name,
:address => address,
:city => city,
:state => state,
:zip => zip,
:location => {
:lat => latitude,
:lon => longitude
}
}.to_json
end
How can I get this to work?

Rails 3: How do I validate to allow blanks ("") but not nil (NULL in database)

In an ActiveRecord (or ActiveModel) I would like the following spec to pass
it { should allow_value("").for(:my_string) }
it { should_not allow_value(nil).for(:my_string) }
I have tried
validates :my_string, {
:length => { :in => 0..255 },
:presence => true,
:allow_blank => true,
:allow_nil => false,
}
and also
validates :my_string, {
:length => { :in => 0..255 },
:allow_blank => true,
:allow_nil => false,
}
But either it allows both "" and nil or none of them.
This works for me
validates :my_string, length: { in: 0..255, allow_nil: false }
If you just want to validate that the field is not null, but don't care about blank/empty strings, this works:
validates :my_string, length: { minimum: 0, allow_nil: false, message: "can't be nil" }
You can try doing:
validates :my_string, exclusion: { in: [nil]}
It is part of the validations themselves in ActiveRecord.
I've tried the others and all of them are very compliated or allow nil too.
You might need to do a custom validation for this:
validates :my_string, :length => { :in => 0..255 }
validate :my_string_is_valid
def my_string_is_valid
self.errors.add :base, 'My string can not be nil' if self.my_string.nil?
end
You can create a simple custom validator (placed in app/validators dir)
class NotNilValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << "must not be nil" if value.nil?
end
end
and then
validates :my_string, not_nil: true
Or maybe:
validates :my_string, :length => { :in => 0..255 }, :allow_nil => false
Seems that allow_nil does not override allow_blank.
So you better no specify allow_blank
This works for me:
validates_exclusion_of :my_string, in: [nil]

Netzke basepack. Need advice with multi uploading fields

Is there any easy way to include the multiupload feature to NetzkeFormView or GridView(AddInForm)?
My current image uloading field with carrierwave is:
{:name => :image_link, :xtype => :displayfield, :display_only => true, :getter => lambda { |r| %Q(<a href='#{r.image.url}'>Download</a>) if r.image.url }},
{:name => :image, :field_label => "Upload image", :xtype => :fileuploadfield, :getter => lambda { |r| "" }, :display_only => true}