snusnu / dm-accepts_nested_attributes Goto Github PK
View Code? Open in Web Editor NEWA DataMapper plugin that allows nested model assignment like activerecord.
License: MIT License
A DataMapper plugin that allows nested model assignment like activerecord.
License: MIT License
dm-accepts_nested_attributes 1.0.0 requires dm-core ~> 1.0.0. :(
I'm trying to use dm-accepts_nested_attributes, but wondering if the example in the README is even working.
Suppose I define the model given in the readme, shouldn't I expect, if I define a person , p, with a profile, that p.profile_attributes return the attributes of the profile?
It seems to just return nil.
I'd like to propose a change to DataMapper::NestedAttributes::Resource#update_or_mark_as_destroyable
. That method ends by saving the association object. I think that's incorrect and the line should be deleted. Here's why.
If I call @object.attributes = some_hash
, I'm explicitly saying "update the attributes but don't save yet." If I wanted to update and save at the same time, I'd call @object.update(some_hash)
. This line breaks that paradigm: if the hash contains attributes for associations, this method automatically saves them.
This is broken because:
attributes=
without saving the associations; update
and attributes=
do the same thing as far as nested resources are concernedIt is important to note that #save will save the complete loaded
object graph when called. This means that calling #save on a
resource that has relationships of any kind (established via
belongs_to or has) will also save those related resources, if
they are loaded at the time #save is being called. Related
resources are loaded if they've been accessed either for read or
for write purposes, prior to #save being called. contains
attributes for nested resorces
Model.raise_on_save_failure
setting, because as soon as one of these associations fails to save, the process of building the others is aborted.For example, in a Rails controller, I want to be able to do:
@foo.attributes = params[:foo_attributes]
begin
@foo.save
redirect_to someplace
rescue
render :new, alert: t('flash.save_failed')
This lets me rescue if any association failed to save, without having to manually check them all. However, if attributes=
tries to save each associated object before I call @foo.save
, the first invalid one short-circuts the process and the other objects never get built, which means they can't be displayed with errors when we re-render the form.
For all these reasons, I think this line should simply be deleted.
If this change is made, @object.attributes = attribute_hash
will set up associated objects but not save them, and @object.update(attribute_hash)
will both set them up and save them. In other words, the associations will behave just like the object itself.
Thoughts?
but creating new association object does.
post.comments_attributes = [{:id => 1, :body => 'blah'}]
post.title = 'new title'
post.save #update to comment is not in the transaction block
post.comments_attributes = [{:body => 'blah'}]
post.title = 'new title'
post.save #both update to post and insert to comment are in the same transaction block.
thank you.
I noticed that dm-accepts_nested_attributes requires dm-core ~> 1.0.0. However, I need to use dm-accepts_nested_attributes with dm-core 1.2.0.
I may well be using this wrong, or maybe this is intentional, but I have code like the code below, and for some reason, it's requiring me, in the Tag#update action, to manually set params[:tag][:tag_controller_attributes][:tag_id]
- is that normal?
It seems like that should be something DANA could set automatically.
class Tag
[...]
has 1, :tag_content
accepts_nested_attributes_for :tag_content
[...]
end
class TagContent
[...]
belongs_to :tag
[...]
end
class TagsController < ApplicationController
def edit
[...]
@tag.tag_content ||= TagContent.new
[...]
end
[...]
end
// edit.html.erb
<%= form_for @tag do |f| %>
[...]
<%= f.fields_for @tag.tag_content do |content_form| %>
[...some fields...]
<% end %>
<% end %>
parent.saved? = true
parent.id = some id
when the rollback occurs, parent.saved? should be false.
if the child validation fails, then parent after save and after create callback should never execute.
The branch https://github.com/snusnu/dm-accepts_nested_attributes/tree/release-1.2 has a version 1.1.0 but rubygems doesn't know it.
Can you push this version on rubygems ?
Code is more descriptive than any word:
@person = Person.first
@person.tasks = [{:id => 1, :_delete => true}]
@person.save
And task is still there
@person = Person.first
puts @person.tasks # Just preload it
@person.tasks = [{:id => 1, :_delete => true}]
@person.save
Now it works as expected.
Hi, do someone have an example of using this gem in a working application ?
I'm getting the following exception when trying to assign nested attributes for a new parent record:
undefined method '[]' for nil:NilClass
This doesn't happen when updating an existing parent record with nested attributes.
The application trace identifies this line in my controller: @contract.attributes = params[:contract]
The error originates here: /lib/dm-accepts_nested_attributes/resource.rb:270:in 'reject_new_record?'
That line looks like this:
guard = self.class.options_for_nested_attributes[relationship.name][:reject_if]
The problem appears to be that self.class.options_for_nested_attributes
is an empty hash, so [relationship.name]
returns nil, and nil[:reject_if]
raises undefined method '[]' for nil:NilClass
.
I'm using this gem at commit 09a9eff.
I don't know whether self.class.options_for_nested_attributes
should contain anything or not. Is this something I'm supposed to set? I don't see anything about it in the README.
I cant seem to delete children
The Model:
class Site
has n, :pages
accepts_nested_attributes_for :pages
The View:
= f.fields_for :pages do |p|
...
= link_to 'Remove', '#'
p.hidden_field('_destroy', :value => 0)
If I omit that last line I can add child models, but whenever I have it in there I get:
The attribute '_destroy' is not accessible in Page
Am I using the wrong attribute for deletion? I had assumed it would be identical to activerecord
Hi,
I have a question: How can I install this gem with datamapper 1.2? When in install the gem with "gem instal ..." I see that the master branch depends on datamapper < 1.2.
Thanks in advance,
pcdoc80
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.