Git Product home page Git Product logo

dm-accepts_nested_attributes's People

Contributors

adelyte-chris avatar angelic avatar emmanuel avatar gix avatar postmodern avatar roovo avatar snusnu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dm-accepts_nested_attributes's Issues

Is the example in the README current?

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.

Resource#update_or_mark_as_destroyable should not save

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:

  • I no longer have the option of using attributes= without saving the associations; update and attributes= do the same thing as far as nested resources are concerned
  • Saving here is unnecessary anyway. If we don't save, calling save on the parent object will save these associations anyway, per the DataMapper documentation:

It 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

  • Finally, saving here makes it impossible to use DataMapper's global 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?

association update is not wrapped inside transaction

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.

Incompatible with dm-core 1.2.0

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.

Child model's parent_id attribute isn't set for has 1 associations.

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 %>

Deleting won't work on edge

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.

rails3 example app

Hi, do someone have an example of using this gem in a working application ?

Error - options_for_nested_attributes is empty hash

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.

Destroying child models for has n

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

Datamapper 1.2

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.