New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix accepts_nested_attributes_for #14
base: master
Are you sure you want to change the base?
Conversation
context "when accepts_nested_attributes_for" do | ||
before { User.accepts_nested_attributes_for :posts } | ||
|
||
it { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using {...}
for multi-line blocks.
before { User.accepts_nested_attributes_for :posts } | ||
|
||
it "set association" do | ||
user = User.new(:email => "t@1.com", :posts_attributes => { "0" => {:title => "This is title!"}}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [105/80]
Use the new Ruby 1.9 hash syntax.
Space inside } missing.
Space inside { missing.
不能改变生成 Id 的时机,自增 Id 不同于 ObjectId 是有限的空间,new 就用掉会浪费掉,还有很多连带的未知问题 |
想想别的方法 |
也是这个道理。不过目前没有想出什么更好的办法,我再想想吧 |
accepts_nested_attributes_for 是使用 def autosave(metadata)
if metadata.autosave? && !metadata.embedded?
save_method = :"autosave_documents_for_#{metadata.name}"
define_method(save_method) do
if before_callback_halted?
self.before_callback_halted = false
else
__autosaving__ do
if relation = ivar(metadata.name)
options = persistence_options || {}
if :belongs_to == metadata.macro
relation.with(options).save if changed_for_autosave?(relation)
else
Array(relation).each { |d| d.with(options).save if changed_for_autosave?(d) }
end
end
end
end
end
after_save save_method, unless: :autosaved?
end
end |
User 保存的时候不会自动保存关联 id. 研究了 mongoid 的代码发现, mongoid 在 Model.new 的时候,id 就已经设置值了,而 mongoid_auto_increment_id 在调用 as_document 时才会设置 id ,所以出现关联 id 不会自动保存的问题。