See the code and what I mean in the picture
how do i show the parent in the child index page?
An example is the ID number 1,2,3,5 they are the child and their parent is the account name OSCAR. and for the parent THIRD their ID child number is 6 and 7.
I can show the child in the parent show page but I dont know how to display them oppositely.
like this is working if my parent want to display child
but I want a child index showing all the child and with its parents
This looks like a Data integrity issue to me. Open a rails console with rails c and then type Remittance.find_by(id: 4).invoice, it will either be that is nil as the objects aren't properly associated, or Remittance.find(id: 4).invoice.account_name will be "" or nil, as that invoice's account_name doesn't have anything populating it.
EDIT:
To fix the problem:
You need to associate your database objects in the model with a has_many/belongs_to relationship. With a SQL DB, this is done with a foreign key. Here's the documentation: http://guides.rubyonrails.org/association_basics.html
Related
I'm trying to create multiple items (associated classes) from the parent class to the very child one (4 level deep) in a single workflow. I'd like to be able to create the parent item, then click on "next" to be able to create one/many children, then click on "next" to create the children of the children, and so on... Finally, in the last screen, i'd like to be able to save all the items by clicking on save. If something is missing in the child item, the parent class cannot be created as well.
Do we have any ideas of how i can manage to do that?
Many thanks :)
You can use accepts_nested_attributes_for in the parent model to associate the child models,.
In the view you can use fields_for or simple_fields_for (if simple_form gem is used) to list the child model fields in the subsequent steps,.
In every step rather than saving the object in the db, you can check if the object is valid or not using .valid? instead of save. At the final step you can use .save method.
With .valid? you can get the errors at each step and at the final step you can save all the records by creating the parent object.
I have a parent model that has many child models
I want to destroy duplicate child records based on one parameter of the child.
I tried this:
parent.child.uniq! {|child| child.parameter }
It apparently works but the database does not reflects the change.
Your operation is performed on an object retrieved from database and not the actual database. Check out this post for the right answer.
I have one form in AX 2012 from which i am calling another child form which shows records that are related to the selected record in Parent form(Dynamic linking).
Now, when I am trying to create a new record in a child form, the dynamic linking between this form and its parent form gets cleared and child form starts showing all data according to its datasource and not any filtered records which I don't want.
I want to know that, how to get stick with that dynamic linking while creating new Record in a child form.
Check that your grids really belongs to the correct datasources. If not, strange things happen.
I'm pretty new to using MVC3 so hopefully the terms I'm using are all correct.
Basically I'm looking for a way (if possible!) to have a parent and child(s) record be added on the same page. I'm able to get the page to display correctly, but I'm unsure how to update the parent model.
In my Create view I have all the fields for the parent, and I'd also like to add the child records to the form as well.
I'm currently using Html.RenderPartial("CreateChildView") to display my Create child page within the parent.
I'm also using Html.RenderPartial("IndexChildView) to display my child records that have been added.
Where I'm stuck at is how to update the Parent model from the "CreateChildView" from above,
I've tried adding a submit button to the child view but I'm not getting the results I was hoping for.
the equivelent c# code that I'm kind of looking for is:
ParentObject.ChildObject.Add(new ChildObject)
and then a way to display this correctly
Thanks in Advance
Place both partials in div with ids. Post data with ajax from "CreateChildView" and update "IndexChildView" with response.
I have some models all linked together in memory (parent:child:child:child) and saved at the same time by saving the top-most parent. This works fine.
I'd like to tap into the after_create callback of one of the children to populate a changelog table. One of the attributes I need to copy/push into the changelog table is the child's foreign_key to it's direct parent, but it doesn't exist at the time after_create fires!?!
Without the after_create callback, I can look in the log and see that the child is being saved before it's parent (foreign key blank) then the parent is inserted... then the child is updated with the id from the parent. The child's after_create is firing at the right time, but it happens before Rails has had a chance to update the child with the foreign_key.
Is there any way to force Rails to save such a linkage of models in a certain order? ie.parent, then child (parent foreign_key exists), then that child's child (again, foreign_key is accessible) etc. ?? If not, how would I have my routine fire after a record is created AND get the foreign_key?
Seems a callback like this would be helpful: after_create_with_foreign_keys
Since I was building all my associated models in memory and relying on Rails to save everything when I saved the top-most parent, I couldn't tap into the after_create callbacks and utilize a foreign key (for a change_log table entry) because of the order in which Rails would save the models. Everything always ended up connected in the proper way, but sometimes a child record would be saved first, then the parent, then an update to the child record to insert the parent_id would happen.
My solution was to not build my models in memory, abandoning the idea of saving everything in one fell swoop. Instead, I would save the top-most model and then create its child via the after_create of that parent. That child's after_create would then create its child and so on. I like this arrangement much better as I have more control over my callbacks in relation to foreign keys. Lastly, the whole thing was wrapped in a db transaction so as to undo any inserts if something went horribly wrong along the way. This was my original reason for building everything in memory, so I'd have all my ducks in a row before saving. Model/db transactions alleviates the worry.
Could you use after_update to catch the child after the parent_id is available? When after_update fires, the parent_id will be available, so if the child is not in the table, insert it.