Home > ruby on rails 4 > Trying to update nested model first on before_save

Trying to update nested model first on before_save

February 11Hits:1
Advertisement

I am calculating :price in my nested item model in order to map it in my invoice model. The problem is that the before_save in the invoice model is being called first and I want it to be called after the before_save in the nesteditem model

Any ideas on how to do this?

Here are my models

invoice.rb

class Invoice < ActiveRecord::Base belongs_to :user has_many :items  accepts_nested_attributes_for :items, :reject_if => :all_blank, :allow_destroy => true  validates :sender, presence: true  before_save :increment_invoice_number, :set_amount  private  def increment_invoice_number     if published == true         self.invoice_number = user.invoices.where(:published => true).count + 1     end end  def set_amount     self.amount = items.map(&:price).sum(&:to_i)              end  end 

And item.rb

class Item < ActiveRecord::Base  belongs_to :invoice   before_save :set_price    def set_price     self.price = cost.to_i * quantity.to_i   end end 

Answers

I added validates_associated :items to invoice.rb and then changed before_save in my nested model to before_validation and also added validates_presence_of :price and this fixed it.

Related Articles

  • Trying to update nested model first on before_saveFebruary 11

    I am calculating :price in my nested item model in order to map it in my invoice model. The problem is that the before_save in the invoice model is being called first and I want it to be called after the before_save in the nesteditem model Any ideas

  • EF update in nested models through DBFebruary 10

    I already have a model which includes different object models in it. When I try to make modifications in those object models update function that I provide does not work as it is supposed to. I encounter with problems in DB. My existing classes that

  • Rails - Why does my nested model form not require 'accepts_nested_attributes_for'February 9

    I have two models item and user_item. Item has many user_items and user_items belongs to item. I have a form where a user can create a new item. In the form a user should include a picture. The name, description, and tags get saved to a new item obje

  • rails with most nested models and cocoon gem;

    rails with most nested models and cocoon gem;February 16

    I have a rails4 app with nested model form. There is a product model that has_many product_features, has_many product_usecases and has_many product_competitors. Everything works fine except when I load the page only the first nested model (product_fe

  • Before validation on nested modelJanuary 9

    I have a nested model items and I am trying to multiply two columns together cost and quantity to set the last column price. I need to set the column price before the form is saved and i need to also validate the model. The before_validation call bac

  • How easy it is to update Entity Model from database when enough code has already been written?February 20

    I am new to Entity Framework. I know there is an option to update model from the database, but I want to know how easy and successful is this task. I may need to update my model in a few weeks but till that time many lines of code has already been pu

  • Nested Iteration with model builder - using nested models

    Nested Iteration with model builder - using nested modelsSeptember 7

    I'm new to using model builder and have to do a nested iteration (for every month I have to iterate through 6 parameters). I am thinking of doing that by using nested models. One model doing the interation through monthes and then providing the curre

  • PHP MVC class with controller and nested model

    PHP MVC class with controller and nested modelNovember 26

    I had asked a first question about this class a while ago and got a few answers here which made me rewrite it completely. I removed all statics and globals, added my variables as arguments for the constructor, and pass them again as reference through

  • Problem validating parent model with nested models in modelbuilder

    Problem validating parent model with nested models in modelbuilderMarch 24

    I have a parent model with several nested models. I'm having trouble getting the parent model to validate. The issue is with sub model 4. The two screenshots are of the parent model and sub model 4. The output from sub model 3 feeds into parameter "M

  • Using global parameters in nested models of ModelBuilder?October 22

    I have a model that consists of two nested models and various parameters. There are two parameters that are common to all of the models (including the overall "grand" model). The common parameters are in-line variables and serve to assign unique

  • Ember.js 2.2 - How to update Route model from a Component?January 14

    With Ember 2.2.0, I have a Route: Dash.BacklogReportsRoute = Ember.Route.extend model: -> @store.queryRecord "report", newest: true and a Component: Dash.BacklogReportComponent = Ember.Component.extend report: null actions: generate: -> #

  • Rails routes for self nested modelsJanuary 15

    In my rails app I have a Task model. In my app, my tasks can have substasks and so on. Since my subtasks are tasks with only a parent task, I did a self-nested model such as class Task < ActiveRecord::Base ## Self Join has_many :subtasks, class_name:

  • Yii2 update related model makes insert

    Yii2 update related model makes insertJanuary 18

    Depending on this question: Yii2 updating two related models does not show data of the second. I have manged calling the related model InvoiceItems to the Invoices model it hasMany relation. However, updating leads to insert new records in invoice_it

  • Creating Instance of Nested model inside parent controllerJanuary 18

    I would like to know if it is possible to create an instance of a nested model inside the parents controller. Let me explain. I have a model called Museum and another model called Museumimage. Museumimage has only one field which is an attachment, ma

  • Knockout JS Mapping fromJS nested modelsJanuary 27

    I am having trouble understanding how to work with Knockout JS Mapping Plugin. I have some nested models (as seen below) and what I am currently doing is just using the ko.mapping.fromJS() in the parent model. What I am noticing is that computed valu

  • Rails nested models and virtual attribute initializationJanuary 27

    I have a problem understanding how are attributes "sent" to nested model(s), and if is possible to do this for model with virtual attrubute too. I have three models: class User < ActiveRecord::Base ... has_and_belongs_to_many :clearancegoods

  • How to update the model class in Django with values submitted to a form in Bootstrap Modal windowFebruary 8

    How to update the model class(database) in Django with values submitted to a form in Bootstrap Modal window Models.py from __future__ import unicode_literals from django.db import models class task_item(models.Model): task_name = models.CharField(max

  • checkbox updating ng-model of another fieldFebruary 14

    I am trying to use a checkbox to set the date for another input field. I can't make it work. Any help would be appreciated. I have created a jfiddle to demonstrate what I am trying to do. As you can see the checkbox does enter the date, but doesn't c

  • How to use directive-defined events to update my modelFebruary 16

    I am creating drag and drop functionality by creating a <dragItem> directive and a <droptTraget> directive, but I don't understand yet how to work with the inner and out scope in this way. Here are my directives. The events triggers the functi

  • django form save can't be used update a model instance?February 17

    I'm using django 1.8.4 djanog.forms.models.BaseModelForm.save reads as following def save(self, commit=True): """ Saves this ``form``'s cleaned_data into model instance ``self.instance``. If commit=True, then the changes to ``instance`` wil

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.634 s.