Rails 7 - ActiveRecord::Associations::Preloader.new.preload - ruby-on-rails-6

consider this code:
# in Rails 6.1
def preload(resource, relations)
ActiveRecord::Associations::Preloader.new.preload(resource, relations)
end
So:
I want to change that for compatibility with Rails 7 so I wrote this:
def preload(resource, relations)
ActiveRecord::Associations::Preloader.new(records: resource, associations: relations)
end
Did I do a right thing? because .preload(resource, relations) is not exit in Rails 7 anymore.
if you have any other suggestion I'm so looking forward for it

You're almost there. It looks like this works:
ActiveRecord::Associations::Preloader.new(
records: [resource].flatten, # in case if resource is a single ApplicationRecord object
associations: relations
).call

Related

How to do file over-rides in hydra?

I have a main config file, let's say config.yaml:
num_layers: 4
embedding_size: 512
learning_rate: 0.2
max_steps: 200000
I'd like to be able to override this, on the command-line, with another file, like say big_model.yaml, which I'd use conceptually like:
python my_script.py --override big_model.yaml
and big_model.yaml might look like:
num_layers: 8
embedding_size: 1024
I'd like to be able to override with an arbitrary number of such files, each one taking priority over the last. Let's say I also have fast_learn.yaml
learning_rate: 2.0
And so I'd then want to conceptually do something like:
python my_script.py --override big_model.yaml --override fast_learn.yaml
What is the easiest/most standard way to do this in hydra? (or potentially in omegaconf perhaps?)
(note that I'd like these override files to ideally just be standard yaml files, that override the earlier yaml files, ideally; though if I have to write using override DSL instead, I can do that, if that's the easiest/best/most standard way)
It sounds like package override might be the a good solution for you.
The documentation can be found here: https://hydra.cc/docs/next/advanced/overriding_packages
an example application can be found here:
https://github.com/facebookresearch/hydra/tree/master/examples/advanced/package_overrides
using the example application as an example, you can achieve the override by doing something like
$ python simple.py db=postgresql db.pass=helloworld
db:
driver: postgresql
user: postgre_user
pass: helloworld
timeout: 10
Refer to the basic tutorial and read about config groups.
You can create arbitrary config groups, and select one option from each (As of Hydra 1.0, config groups options are mutually exclusive), you will need two config groups here:
one can be model, with a normal, small and big model, and another can trainer, with maybe normal and fast options.
Config groups can also override things in other config groups.
You can also always append to the defaults list from the command line - so you can also add additional config groups that are only used in the command line.
an example for that can an 'experiment' config group. You can use it as:
$ python train.py +experiment=exp1
In such config groups that are overriding things across the entire config you should use the global package (read more about packages in the docs).
# #package _global_
num_layers: 8
embedding_size: 1024
learning_rate: 2.0

Obtaining the QAST of a Perl 6 file from another program

This is related to this question on accesing the POD, but it goes further than that. You can easily access the Abstract Syntax Tree of a Perl 6 program using:
perl6 --target=ast -e '"Þor is mighty!".say'
This will print the whole Q abstract syntax tree. It's not too clear how to make this from your own program, or I haven't found how to do it. In fact, the CoreHackers::Q module runs that as an external script. But being able to access it from your own program, like
use QAST; # It does not exist
my $this-qast = QAST::Load("some-external-file.p6") # Would want something like this
would be great. I'm pretty sure it should be possible, at the NQP level and probably in a Rakudo-dependent way. Does someone know hot it goes?
Since QAST is not a part of the Perl 6 language specification, but an internal implementation detail of Rakudo, there's no official way to do this. Eventually there will be an AST form that is part of the language specification, but that doesn't yet exist (the 007 project which is working on exploring this area).
It is, however, possible to obtain the QAST tree by using:
use nqp;
my $ast = nqp::getcomp("perl6").eval("say 42", :target<ast>);
say $ast.dump();

Laravel error on merge collection later to upgrate from 4.0 to 4.1

Later to upgrade Laravel version i found that the Collection::merge method isn't working well.
Not sure if it is my problem, i can't find an error. Lets see some information:
print_r($ecb->count());
print_r($boc->count());
// merge both
$cubes = $ecb->merge($boc);
print_r($cubes->count());
dd();
output:
36 27 1
the merge should to give like output 36 + 27 (there isn't duplicate element on the collection)
More debug information:
print_r($ecb->toArray());
print_r($boc->toArray());
// merge both
$cubes = $ecb->merge($boc);
print_r($cubes->toArray());
dd();
output (is a bit long): http://laravel.io/bin/PdVj1#7
Any idea?
Thanks
Yes - it appears to have changed between 4 and 4.1
See this Github issue: https://github.com/laravel/framework/issues/3445
In essence Eloquent collections, upon merging, remove models with duplicate primary keys.
I'm running Laravel 4.1.29 - and I get a different output to you with count() - but in essence it just removes duplicate ids.
I see that in Laravel 4.1 merge delete element with same ids ( https://github.com/laravel/framework/issues/3445 )
To have the same behavior i should to change the code like it:
$boc->each(function($cube) use ($ecb)
{
$ecb->push($cube);
});
The merge function uses Model#getKey() to differentiate different models - do the models you are using have a primary key specified properly? I notice they don't have the standard id field.

How to solve collective.js.jqueryui versioning for both Plone 4.2 and 4.3

I have an addon which I want to update to be compatible with Plone4.3, but which should remain functional when Plone4.2 users do an upgrade.
The readme says this:
For Plone 3 you need version 1.7.x of this package
Plone < 4.3 Use version < 1.9
How can I configure this kind of version-specific dependency in setup.py?
Actually it's c.js.jqueryui which should pin the Plone-versions in their tag-releases, IMHO.
So you should contact the authors probably.
To be consequent, you should add 'Products.CMFPlone' as a dependency, too, but I don't know what is best practice here.
Anyway, to answer your question, here is a possibilty to distinct, whether we have a Plone-3 or not:
In your setup.py add at the top
import glob
# BBB: Append Plone-3-specific version-pinnings
# to `install_requires`, in case third-party-eggs
# do not specify their requirement-versions.
# Assumes, Plone-eggs are in the same directory
# as this egg, which is given, if fetched of pypi.
# Otherwise assumes, you know how to tweak
# buildout and pin versions yourself.
# Collect P3-eggs in eggs-directory:
plone3_eggs = glob.glob('../Products.CMFPlone-3*')
# Found something?
if len(plone3_eggs) > 0:
# Expects `install_requires=[ ... ] + plone3_requires,`
# in setup-method below, to take effect:
plone3_requires = [
'collective.js.jqueryui<=1.7',
# Add more P3-pins here.
]
And install_requires in the setup-method, gets this:
install_requires= [
'setuptools',
'collective.js.jqueryui',
] + plone3_requires,

Ploneformgen and regular expression

I would like to use regular expression in the form created using ploneformgen 1.7. I am using plone 4.1. In the custom validation of a text field.
How do I use the regular expression?
I tried the following:
python: import re; test(value==re.search(r'[123]'), False, 'Needs to be number 1 or 2 or 3')
but it gives me error and hence cannot validate.
I want value should be 1 or 2 or 3. I am able to use the expression as
python: test(value=='1' or value=='2' or value=='3', False, 'Needs to be number 1 or 2 or 3')
BUT I would like to use regular expressions. Please guide.
The set of packages and types that you may use in through-the-web scripts, TALES "python:" expressions, PFG overrides and PFG script adapters is limited to those available in Restricted Python. Restricted Python is deliberately very conservative in which modules and types it makes available.
You may expand the list with explicit "allow" declarations made in a Python package included in your egg list. A sample of such a package is available in the github collective, and includes several modules and types that I've found particularly useful in PloneFormGen, including re.
You will still not be able to "import re" in a TALES expression, but you will be able to use re in a TTW script, which may be used as a PFG validator.
Per http://docs.zope.org/zope2/zope2book/ScriptingZope.html, you cannot use regular expressions in TALES, since they are forbidden by Restricted Python.
You actually need 2 parameters for the search method.
You should use this way: import re; test(re.search(r'[123]', value), False, 'Needs to be number 1 or 2 or 3')

Resources