Home > validation > Before validation on nested model

Before validation on nested model

January 9Hits:1
Advertisement

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 back obviously breaks if the cost and quantity is not set before saving. Any ideas of how I can multiply these columns together and also validate the model?

Here is my code item.rb

class Item < ActiveRecord::Base belongs_to :invoice   validates :invoice_id, presence: true  validates :name, presence: true  validates_presence_of :quantity, :cost, :price   before_validation :set_price   def set_price     self.price = cost * quantity.round(2)  end end 

and here is my parent model 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 validates_associated :items  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              end 

end

Related Articles

  • 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

  • form validation with nested models in Rails

    form validation with nested models in RailsJanuary 22

    I have this problem. I need to validate the attributes of two models in the same form in Rails. One is the parent of the other. The form is like this: <%= semantic_form_for @professional do |pro| %> <%= pro.inputs :id => "information"

  • 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

  • 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

  • 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

  • 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

  • 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:

  • 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

  • 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

  • Rails: nested models disappearing after validationJanuary 26

    I have a model: class Invoice < ActiveRecord::Base belongs_to :case has_many :invoice_positions accepts_nested_attributes_for :invoice_positions, allow_destroy: true And i have a form createn with https://github.com/nathanvda/cocoon but i have one sm

  • Creating nested models in Rails 4 forum appJanuary 31

    Hello I am making a Forum application in Rails 4. It can have numerous forums, each with numerous topics. Each topic can have many posts. When creating a new topic, one must also create the initial post, much like Stack Overflow itself. Therefore, I

  • Rails - Validating complex nested formFebruary 10

    I have a form with four input fields where a user creates an item. In the form one of the four input fields is for picture which is used to create a user_item at the same time on form submission. I am running into several problems. When validation fa

  • Where we should put validation for domain modelNovember 15

    I still looking best practice for domain model validation. Is that good to put the validation in constructor of domain model ? my domain model validation example as follows: public class Order { private readonly List<OrderLine> _lineItems; public vi

  • Model builder - Nesting models to extract raster values to points

    Model builder - Nesting models to extract raster values to pointsFebruary 23

    Hello! Hopefully a quick question. I have a simple model below that iterates through a folder full of rasters, taking each in turn to extract data values based on a single shapefile of Random point locations. The rest works over the points table to a

  • Drupal validation for nested field elementMarch 25

    I have written some custom validation for each element in a form. I use the following method to display errors. It automatically highlights the required fields. form_set_error('field_athletes_male', 'Athletes Male field is required.'); My problem is

  • Validation Logic In ModelJanuary 14

    I was debating putting validation logic in my models (setter functions). Is this something I should be using a separate object for? Below is something I had in mind. class Person { private $name, $setErrors = []; public function setName($name){ if(st

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