mongoid.update_attributes fails on new embedded object - initialization

I try to create a new embedded object with update_attributes from its parent. In mongoid 2.0.0.beta20 this worked fine, but after updating to 2.3.3, this fails.
To reproduce, you can use this model structure:
class Bird
include Mongoid::Document
embeds_one :egg
accepts_nested_attributes_for :egg
end
class Egg
include Mongoid::Document
field :weight, :type => Integer, :default => 10
field :size, :type => Array, :default => [10,10]
def height=(value)
self.size[0] = value
end
def width=(value)
self.size[1] = value
end
def height
self.size[0]
end
def width
self.size[1]
end
end
Then, run in the ruby console:
b = Bird.new
=> #<Bird _id: 4eb163660d996a3e43000002, _type: nil>
b.update_attributes "egg" => { "height" => 20, "weight" => 20 }
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=
from (irb):14:in `height='
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:98:in `process_attribute'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:26:in `block in process'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:24:in `each_pair'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:24:in `process'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/document.rb:131:in `block in initialize'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/document.rb:127:in `initialize'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/factory.rb:23:in `new'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/factory.rb:23:in `build'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/relations/builders/nested_attributes/one.rb:30:in `build'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:137:in `block in process_relations'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:134:in `each_pair'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:134:in `process_relations'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:122:in `process_pending'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes/processing.rb:29:in `process'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes.rb:133:in `block in assign_attributes'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/threaded/lifecycle.rb:26:in `_assigning'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes.rb:132:in `assign_attributes'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/attributes.rb:154:in `write_attributes'
from vendor/ruby/1.9.1/gems/mongoid-2.3.3/lib/mongoid/persistence.rb:115:in `update_attributes'
from (irb):36
from vendor/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from vendor/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from vendor/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'system :037 >
This is because the embedded object does not get initialized before the new values are set. That's why the size array is not initialized and is therefore still nil.
Using mongoid-2.0.0.beta20 (from which I upgrade), all works fine.
Is there something I miss? Does the new mongo-2.3.3 expect ME to initialize the embedded object first!?
Thanks for any help|hint.

Defaults are now applied after all the other attributes are being set, in case you were to use a proc as the default that conditionally depended on the others. Your height= and width= methods need to be aware of this now.
See:
https://github.com/mongoid/mongoid/issues/1075

Related

ERROR: LoadError: UndefVarError: type not defined

I am working in Julia and I'm trying to use Documenter.jl to create a documentation page for Examples.jl and I keep getting this error when I try to run my make.jl file:
ERROR: LoadError: UndefVarError: type not defined
Here is my code for the make.jl file:
using Documenter
using Examples
makedocs(
sitename = "Examples.jl",
pages = Any[
"About" => "index.md",
"Main" => "main.md",
"Graphs" => "graphs.md",
"Utilities" => "utilities.md",
"Tutorial" => "tutorial.md"
]
)
Any ideas how to fix this error? Thanks
As #Matt B. pointed out, type is no longer a valid keyword. In Julia 0.x you used to define types as:
type foo
bar
end
Now we use struct or mutable struct instead. e.g.
struct foo
bar
end
Go through your code and see if you see type anywhere.

Problems running rails db:migrate with rolify

Currently trying to install rolify to my rails application
Followed the steps on github and am getting the following error after running rails db:migrate
/home/alex/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rolify-5.2.0/lib/rolify.rb:30: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/home/alex/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3/lib/active_record/associations.rb:1826: warning: The called method `has_and_belongs_to_many' is defined here
rake aborted!
NoMethodError: undefined method `Migration' for ActiveRecord:Module
/mnt/d/linux-docs/projects/code/rails/kream/kream/src/db/migrate/20200511073629_rolify_create_roles.rb:1:in `<main>'
Caused by:
NoMethodError: undefined method `Migration' for ActiveRecord:Module
/mnt/d/linux-docs/projects/code/rails/kream/kream/src/db/migrate/20200511073629_rolify_create_roles.rb:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Within the migration file ammend the version number like so
class RolifyCreateRoles < ActiveRecord::Migration[6.0]
def change
create_table(:roles) do |t|
t.string :name
t.references :resource, :polymorphic => true
t.timestamps
end
create_table(:users_roles, :id => false) do |t|
t.references :user
t.references :role
end
add_index(:roles, [ :name, :resource_type, :resource_id ])
add_index(:users_roles, [ :user_id, :role_id ])
end
end

Flow type & Styled-components errors when imported

I'm on win 7.
Code:
// #flow
import React from 'react';
import styled from 'styled-components';
Errors:
... many lines of errors
Error: node_modules/styled-components/lib/utils/create-broadcast.js.flow:20
20: listeners.forEach(listener => listener(currentValue))
^^^^^^^^^^^^ unused
function argument
10: subscribe: (listener: () => void) => () => void
^^^^^^^^^^ function type expects no arguments
Error: node_modules/styled-components/lib/utils/create-broadcast.js.flow:26
26: listener(currentValue)
^^^^^^^^^^^^ unused function argument
10: subscribe: (listener: () => void) => () => void
^^^^^^^^^^ function type expects no arguments
Error: node_modules/styled-components/lib/utils/getComponentName.js.flow:4
4: export default function getComponentName(target: ReactClass<*>): string {
^^^^^^^^^^^^^ identifier `ReactClass`. Could not resolve name
Found 24 errors
error Command failed with exit code 2
.flowconfig:
[ignore]
./build/.*
./node_modules/.*
[include]
[libs]
[lints]
[options]
suppress_comment=.*\\$FlowFixMe
[strict]
The package.json file is below:
"flow-typed": "^2.2.3",
"flow-bin": "^0.59.0",
"styled-components": "2.0.0",
Used project scaffold from https://github.com/react-boilerplate/react-boilerplate # 3.5.0
I saw in a comment to use libdef 1.4, but when I run yarn add --dev flow-typed#1.4 I only get version 1 or version 2.x.x, but no 1.4.
What to do next?
If you want flow to stop checking node_modules you can either ignore them in .flowconfig or include only your source files.
To ignore everything in node_modules:
[ignore]
.*/node_modules/.*
Pay attention:
These regular expressions match against absolute paths. They probably should start with .*
More info here

There is an error using CMUCL

When staring cmucl with quicklisp, i get the following error:
Error in KERNEL::UNDEFINED-SYMBOL-ERROR-HANDLER: the function GENERATE-CRC32-TABLE is undefined.
[Condition of type UNDEFINED-FUNCTION]
Restarts:
0: [CONTINUE ] Return NIL from load of #P"/home/***/.cache/common-lisp/cmu-19f__19f_-freebsd-x86/home/***/quicklisp/quicklisp/deflate.sse2f".
1: [TRY-RECOMPILING] Recompile deflate and try loading it again
2: [RETRY ] Retry Loading component: ("quicklisp" "deflate").
3: [ACCEPT ] Continue, treating
Loading component: ("quicklisp" "deflate") as having
been successful.
4: Return NIL from load of #P"home:quicklisp/setup.lisp".
5: Return NIL from load of "home:.cmucl-init".
6: [ABORT ] Skip remaining initializations.
Debug (type H for help)
(KERNEL::UNDEFINED-SYMBOL-ERROR-HANDLER "<error finding name>"
#.(SYSTEM:INT-SAP #x47FD70D4)
#<Alien (*
SYSTEM:SYSTEM-AREA-POINTER) at #x47FD6DC0>
(16))
Source: Error finding source:
Error in function DEBUG::GET-FILE-TOP-LEVEL-FORM: Source file no longer exists:
target:code/interr.lisp.
How can i fix it, thanks !
The easiest fix is to use a recent version of CMUCL.

ActiveRecord 3.1 & SQL Exception on Debian

developing on Lion, packaged with sqlite3 3.7.5, i am having no problems.
i pushed to production (Debian Lenny w/ sqlite3 3.5.9) and i get the following SQL Exceptions
SQLite3::SQLException: no such column: taggings.tag_id: [query here]
i confirmed this by running the query manually from the rails dbconsole, and indeed sqlite 3.7.x returns results, but 3.5.x throws an error.
i am confident the query is fine, so is ActiveRecord 3.1 not compatible with sqlite 3.5? I am not seeing a newer version.
any ideas here?
Update
the failing query is...
SELECT tags.*, taggings.tags_count AS count
FROM "tags" JOIN (
SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count
FROM "taggings" INNER JOIN work ON work.id = taggings.taggable_id
WHERE (taggings.taggable_type = 'Work' AND taggings.context = 'tags')
AND (taggings.taggable_id IN(SELECT work.id FROM "work" )
)
GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id
and my schema is
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context"
t.datetime "created_at"
end
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", :force => true do |t|
t.string "name"
end
FIX
well i never discovered the problem, but my assumption is that activerecord 3.1 is not compatible with sqlite 3.5.9.
although the last dpkg version for lenny is 3.5.9, i built 3.7.7 from source and it is processing the queries correctly now.

Resources