Nested Attributes rails 3

316 views
Skip to first unread message

godweed

unread,
Mar 7, 2011, 10:08:28 AM3/7/11
to neo4jrb
Hi Andreas,

Is there any examples for nested attributes ? My code is as below;

has_n(:comments_of).to(Item)
accepts_nested_attributes_for :comments_of , :allow_destroy => true

and I am trying to modified views and controller for nested attributes
usage but not able to. Are there any differences in usage like the way
we done as usual ?

thanks in advance
Ovunc

Andreas Ronge

unread,
Mar 7, 2011, 10:50:38 AM3/7/11
to neo...@googlegroups.com
Hi

Should not be any difference.
Check the rspecs -
https://github.com/andreasronge/neo4j/blob/master/spec/rails/relationship_spec.rb

/Andreas

> --
> You received this message because you are subscribed to the Google Groups "neo4jrb" group.
> To post to this group, send email to neo...@googlegroups.com.
> To unsubscribe from this group, send email to neo4jrb+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/neo4jrb?hl=en.
>
>

godweed

unread,
Mar 22, 2011, 5:16:54 AM3/22/11
to neo4jrb
Hi

I am getting undefined method `build' for #<Neo4j::HasN::Mapping:
0x70390a74> is there any advices ?

has_n(:comments).to(Item)
accepts_nested_attributes_for :comments , :allow_destroy => true

def new
@item = Item.new
@item.comments.build

.....
end

view..........

%h1 New item

= form_for @item do |f|
-if @item.errors.any?
#error_explanation
%h2= "#{pluralize(@item.errors.count, "error")} prohibited this
item from being saved:"
%ul
- @item.errors.full_messages.each do |msg|
%li= msg

.field
= f.label params[:item]
.field_comment
= f.fields_for :comments do |child_form|
= child_form.text_area :comment_text
.actions
= f.submit 'Save'



On Mar 7, 5:50 pm, Andreas Ronge <andreas.ro...@gmail.com> wrote:
> Hi
>
> Should not be any difference.
> Check the rspecs -https://github.com/andreasronge/neo4j/blob/master/spec/rails/relation...

Andreas Ronge

unread,
Mar 22, 2011, 6:13:15 AM3/22/11
to neo...@googlegroups.com
Hi

There is no method build on @item.comments
Instead you should use
@item.comments << Comment.new

I'm currently working on improving the relationships for
Neo4j::Rails::Model, here are some limitations:
* in the example above @item.comments will only return none persisted
relationships (like the added Comment.new) if the @item has not been
persisted (saved)
example, if you are using fields_for and nested attributes you can't
display both persisted and none persisted models at the same time - I
will fix that.
* http://neo4j.lighthouseapp.com/projects/15548/tickets/156-activemodel-support-for-relationships

Maybe I should add the build method as well.

/Andreas

arikan

unread,
Sep 26, 2011, 12:30:22 PM9/26/11
to neo...@googlegroups.com
Hi Andreas, 

Any updates on the "build" method for using with nested_attributes? It'd be handy to make nested forms and keep controllers clean the ActiveRecord way. 

Cheers,
Burak

Andreas Ronge

unread,
Sep 26, 2011, 12:41:55 PM9/26/11
to neo...@googlegroups.com
Yes, looks like it is implemented:
http://neo4j.rubyforge.org/classes/Neo4j/Rails/Relationships/NodesDSL.html#method-i-build
Have to improve the RDoc so that it is easier to find...

> --
> You received this message because you are subscribed to the Google Groups
> "neo4jrb" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/neo4jrb/-/N_x-E65SYBkJ.

arikan

unread,
Sep 26, 2011, 1:16:47 PM9/26/11
to neo...@googlegroups.com
I use neo4j 1.2.2 + rails 3.1, trying nested_attributes using the "build" method, and having an error (below):

class Vendor
   has_n(:products).to(Product)

class Product
   has_n(:vendors).from(Vendor, :products)

vendors_controller.rb

def new
   @vendor = Vendor.new
   @vendor.products.build
    ...
end

returns this error:

wrong number of arguments (0 for 1)


To be able to use "build" for nested_attributes, I guess it should be able to work with 0 arguments, no? Since I can not know the "name" before creating it from my nested form.

Thanks,
Burak

arikan

unread,
Sep 26, 2011, 1:19:23 PM9/26/11
to neo...@googlegroups.com
for the example above, the Vendor class actually accepts nested_attr: 

class Vendor
   has_n(:products).to(Product)
   accepts_nested_attributes_for :products

Peter Neubauer

unread,
Sep 26, 2011, 1:19:48 PM9/26/11
to neo...@googlegroups.com

Guys,
I think it might make sense to pull neo4j ruby into the build server and start running tests against every neo4j build. WDYT?

/peter

Sent from my phone.

> --
> You received this message because you are subscribed to the Google Groups "neo4jrb" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/neo4jrb/-/98bmTy7rvSMJ.

Andreas Ronge

unread,
Sep 26, 2011, 1:53:12 PM9/26/11
to neo...@googlegroups.com
Hi Peter

I already have a build server running around 1300 tests for every commit
http://travis-ci.org/#!/andreasronge/neo4j

/Andreas

Peter Neubauer

unread,
Sep 26, 2011, 1:56:28 PM9/26/11
to neo...@googlegroups.com
Very cool Andreas, didn't know!

I am thinking of running fresh build on Neo4j snapshot builds as well
as Neo4j.rb commits, so we are sure that things are running smoothly
between both. WDYT?

Cheers,

/peter neubauer

GTalk:      neubauer.peter
Skype       peter.neubauer
Phone       +46 704 106975
LinkedIn   http://www.linkedin.com/in/neubauer
Twitter      http://twitter.com/peterneubauer

http://www.neo4j.org               - Your high performance graph database.
http://startupbootcamp.org/    - Öresund - Innovation happens HERE.
http://www.thoughtmade.com - Scandinavia's coolest Bring-a-Thing party.

Andreas Ronge

unread,
Sep 27, 2011, 2:53:51 AM9/27/11
to neo...@googlegroups.com
It is now fixed. It previously only worked if you had an argument,
e.g. @vendor.products.build({})

http://neo4j.lighthouseapp.com/projects/15548-neo4j/tickets/185-rails-has_n-build-method-does-not-need-any-arguments

Notice, there is also a version for creating relationships:
@vendor.products_rels.build
You don't need to declare it with accept_nested_attributes

/Andreas

> --
> You received this message because you are subscribed to the Google Groups
> "neo4jrb" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/neo4jrb/-/hX70EyToz70J.

Andreas Ronge

unread,
Sep 27, 2011, 2:54:19 AM9/27/11
to neo...@googlegroups.com
@Peter, yes sure, why not ?

On Mon, Sep 26, 2011 at 7:56 PM, Peter Neubauer

Burak Arikan

unread,
Sep 27, 2011, 5:25:57 AM9/27/11
to neo...@googlegroups.com
Thanks Andreas, 

But, although it passes @vendor.products.build({}) , gives this error for line 20: 

undefined method `persisted?' for #<Neo4j::Rails::Relationships::NodesDSL:0x71f7efb7>

01:   <%= form_for(@vendor) do |f| %>
20:   <%= f.fields_for :products do |fp| %>
21:     <div class="field">
22:       <%= fp.text_field :name %>
23:     </div>
Simply trying to build a multi-model nested form as usual, any ideas for this "persisted" error?

Burak

Deepak N

unread,
Sep 27, 2011, 6:08:45 AM9/27/11
to neo...@googlegroups.com
Hi Burak,

We had same issue. This is because action-view expects some of these active record association methods on the neo4j association object(NodeDSl). The work-around for this issue is to change line 20 to

20:   <%= f.fields_for :products, @vendor.products.to_a do |fp| %>
It works in rails 3.0.9. Hope it helps!
--
Deepak N

Andreas Ronge

unread,
Sep 27, 2011, 6:19:17 AM9/27/11
to neo...@googlegroups.com
Thanks Deepak.

I committed a possible solution to this problem:
https://github.com/andreasronge/neo4j/commit/40b44abdc58ad7fc60d44a996a0dc47c7050f314
Please test this since I have not written any test yet for it (I will
if it works).

/Andreas

Deepak N

unread,
Sep 27, 2011, 7:42:32 AM9/27/11
to neo...@googlegroups.com
It fixes the above issue. But action-view thinks of association as has_one relation instead of has_n relation. 

Looking at action-view code, looks like it checks if association respond to to_ary to figure out whether it is has_one or has_n association. Adding to_ary method on NodeDSL seem to fix this issue.

        def to_ary
          all.to_a
        end

Andreas Ronge

unread,
Sep 27, 2011, 8:14:51 AM9/27/11
to neo...@googlegroups.com

arikan

unread,
Sep 28, 2011, 1:48:49 PM9/28/11
to neo...@googlegroups.com
Thanks Deepak, this works for me too. -Burak

Dave

unread,
Oct 6, 2011, 1:34:28 PM10/6/11
to neo4jrb
Hi Andreas,

Continuing on with this thread, I'm trying to get Ryan Bates'
nested_form gem working and its looking for the _destroy method and
the reflect_on_association method on the node object when adding
nested attributes. I'm assuming these methods are not implemented in
the Node class. Your recent enhancements fixed other issues I was
having with nested_form_for and fields_for (as mentioned above).

Secondly, another post contained a requirement for adding a new nested
relationship to an existing node e.g. adding a new Contact, which
includes nested address info that references, for example, a Country
node that already exists (versus creating a new Country node). Is this
supported?

These two things will provide me with functionality to easily add new
nested models and remove nested models within the one parent view page
(model) and add a relationship to an existing nested model (probably
need to remove it as well but not destroy the existing nested model
i.e. destroy the relationship).

This recent thread tells me others are working on similar
functionality and may have already solved my issues above (or have
workarounds), or are about to stumble upon them. Any help will be
appreciated since I'm very new with Rails/Ruby/Neo4j.

Thanks,
Dave


On Sep 27, 5:14 am, Andreas Ronge <andreas.ro...@gmail.com> wrote:
> Thanks again, added the methodhttps://github.com/andreasronge/neo4j/commit/527ffc8af46a13fdda6a438f...
>
>
>
> On Tue, Sep 27, 2011 at 1:42 PM, Deepak N <endeep...@gmail.com> wrote:
> > It fixes the above issue. But action-view thinks of association as has_one
> > relation instead of has_n relation.
> > Looking at action-view code, looks like it checks if association respond
> > to to_ary to figure out whether it is has_one or has_n association. Adding
> > to_ary method on NodeDSL seem to fix this issue.
> >         def to_ary
> >           all.to_a
> >         end
>
> > On Tue, Sep 27, 2011 at 3:49 PM, Andreas Ronge <andreas.ro...@gmail.com>
> > wrote:
>
> >> Thanks Deepak.
>
> >> I committed a possible solution to this problem:
>
> >>https://github.com/andreasronge/neo4j/commit/40b44abdc58ad7fc60d44a99...
> >> Please test this since I have not written any test yet for it (I will
> >> if it works).
>
> >> /Andreas
>
> >> On Tue, Sep 27, 2011 at 12:08 PM, Deepak N <endeep...@gmail.com> wrote:
> >> > Hi Burak,
> >> > We had same issue. This is because action-view expects some of these
> >> > active
> >> > record association methods on the neo4j association object(NodeDSl). The
> >> > work-around for this issue is to change line 20 to
>
> >> > 20:   <%= f.fields_for :products, @vendor.products.to_a do |fp| %>
>
> >> > It works in rails 3.0.9. Hope it helps!
>
> >> > On Tue, Sep 27, 2011 at 2:55 PM, Burak Arikan <ari...@gmail.com> wrote:
>
> >> >> Thanks Andreas,
> >> >> But, although it passes @vendor.products.build({}) , gives this error
> >> >> for
> >> >> line 20:
>
> >> >> undefined method `persisted?' for
> >> >> #<Neo4j::Rails::Relationships::NodesDSL:0x71f7efb7>
>
> >> >> 01:   <%= form_for(@vendor) do |f| %>
>
> >> >> 20:   <%= f.fields_for :products do |fp| %>
> >> >> 21:     <div class="field">
> >> >> 22:       <%= fp.text_field :name %>
> >> >> 23:     </div>
>
> >> >> Simply trying to build a multi-modelnestedformas usual, any ideas
> >> >> for
> >> >> this "persisted" error?
>
> >> >>http://guides.rubyonrails.org/getting_started.html#building-a-multi-m...
> >> >> Burak
> >> >> On Tue, Sep 27, 2011 at 9:53 AM, Andreas Ronge
> >> >> <andreas.ro...@gmail.com>
> >> >> wrote:
>
> >> >>> It is now fixed. It previously only worked if you had an argument,
> >> >>> e.g.  @vendor.products.build({})
>
> >> >>>http://neo4j.lighthouseapp.com/projects/15548-neo4j/tickets/185-rails...
> >http://groups.google.com/group/neo4jrb?hl=en.- Hide quoted text -
>
> - Show quoted text -

Andreas Ronge

unread,
Oct 6, 2011, 4:52:34 PM10/6/11
to neo...@googlegroups.com
Hi

On Thu, Oct 6, 2011 at 7:34 PM, Dave <da...@davidcrouchcustomhomes.com> wrote:
> Hi Andreas,
>
> Continuing on with this thread, I'm trying to get Ryan Bates'
> nested_form gem working and its looking for the _destroy method and
> the reflect_on_association method on the node object when adding
> nested attributes. I'm assuming these methods are not implemented in
> the Node class. Your recent enhancements fixed other issues I was
> having with nested_form_for and fields_for (as mentioned above).
>

reflect_on_association is not implemented, not sure what it should
return either.
I have not used the nested_form gem.

> Secondly, another post contained a requirement for adding a new nested
> relationship to an existing node e.g. adding a new Contact, which
> includes nested address info that references, for example, a Country
> node that already exists (versus creating a new Country node). Is this
> supported?

I think so, should be easy to test with IRB ...
You can always create new relationship like this
person.friends_rels.create and person.friends_rels.build or
or if you want it to return and build other node instead:
person.friends.build or person.friends.create

See http://neo4j.rubyforge.org/Neo4j/Rails/Relationships/RelsDSL.html
and http://neo4j.rubyforge.org/Neo4j/Rails/Relationships/NodesDSL.html
and the RSpecs https://github.com/andreasronge/neo4j/blob/master/spec/rails/active_record_style_relationships_spec.rb

Btw, which style of RDoc do you like: horo or darkfish of something else ?

Deepak N

unread,
Oct 8, 2011, 2:30:54 PM10/8/11
to neo...@googlegroups.com
Hi Dave,

Please see comments inline.

On Thu, Oct 6, 2011 at 11:04 PM, Dave <da...@davidcrouchcustomhomes.com> wrote:
Hi Andreas,

Continuing on with this thread, I'm trying to get Ryan Bates'
nested_form gem working and its looking for the _destroy method and
the reflect_on_association method on the node object when adding
nested attributes. I'm assuming these methods are not implemented in
the Node class. Your recent enhancements fixed other issues I was
having with nested_form_for and fields_for (as mentioned above).

The problem you are facing with form with field_for nested attributes can be solved by defining a method on nested model:

  def _destroy
    0
  end

This is expected by the actionview when you add a destroy link with a hidden_filed :_destroy. I haven't used nested_form gem! but this works with rails form builders.
 
Secondly, another post contained a requirement for adding a new nested
relationship to an existing node e.g. adding a new Contact, which
includes nested address info that references, for example, a Country
node that already exists (versus creating a new Country node). Is this
supported?

As you already know unlike relational db, neo4j being a graph db does not track relations by having  associated model id on other models. To be able to associate a reference data, you need to define couple of methods:

class Contact
   has_one :country

   def country_id
      self.country.present? ? self.country.id : nil
   end

   def country_id=(id)
      self.country = Country.find(id)
   end
end

If you need this for multiple models, metaprogramming will help you avoid duplication :) . You should be able to do

class Contact
   has_one :country
   accepts_id_for :country # This wil generate the above getter an setter for country_id
end

And make sure to wrap update within a transaction.



--
Deepak N

Dave

unread,
Oct 17, 2011, 1:09:44 AM10/17/11
to neo4jrb
Hi Deepak, Andreas,

Thanks for all your help to date. I have my code working but came
across a couple of things that you may want to include in upcoming
neo4j.rb releases:-

1) For a many-to-many relationship, the child node is deleted when
calling delete. For Active Record, it appears just the association is
deleted in a many-to-many relationship and the nested record itself is
preserved, which makes sense because it has multiple "owners". I think
neo4j.rb should follow this convention.
2) I'm using backbone.js on the front end and am passing json back and
forth. I noticed that the json representation passed to the client did
not include the node id in the record (I'm using rails-backbone gem) .
The easy workaround was to add "property :id" to the model. However, I
assumed the node id would be included in the json string regardless if
explicity stated in the model.
3) To remove the root from the json string there is a switch in Active
Record called include_root_in_json. Rails 3.1 includes the following
code in the initializer "wrap_parameters.rb":-

ActiveSupport.on_load(:active_record) do
self.include_root_in_json = false
end

With Neo4j configured the above code is not triggered due to
the :active_record hook not being present (or that appears to be the
case). I got it working by setting the switch in each mode class
(rather than initializer). What hook should I use in place
of :active_record?

Lastly, I've been out of the programming world for a number of years
and am now getting back into it. I'm new to ruby/rails/neo4j so I
could easily be doing something wrong in relation to the above. Also,
Andreas, I have no preference on horo or darkfish because I don't know
what they are. Sorry.

Thanks,
Dave


On Oct 8, 11:30 am, Deepak N <endeep...@gmail.com> wrote:
> Hi Dave,
>
> Please see comments inline.
>
> > > >http://groups.google.com/group/neo4jrb?hl=en.-Hide quoted text -
>
> > > - Show quoted text -
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "neo4jrb" group.
> > To post to this group, send email to neo...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > neo4jrb+u...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/neo4jrb?hl=en.
>
> --
> Deepak N- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages