nested-attributes

Custom validation on nested attributes

跟風遠走 提交于 2021-02-19 04:03:08
问题 I need to find a way to get rails (3.2.8) to save nested attributes before performing validation on the parent object. I've searched a long time for an answer to my problem and, while I've found similar questions, I haven't yet seen an elegant solution. Here's the situation: I have an activity_log where each log_entry has multiple activities. The log_entry has a field called 'hours', which represents the total time worked that day. Each activity also has an 'hours' field, representing time

Rails 5 - save rolls back because nested models parent model is not being saved before child model

天大地大妈咪最大 提交于 2021-02-07 09:33:39
问题 Ok folks, Rails 5 has really had its nuances differing from Rails 4. What I have going on is that every time I click the submit button on the form it reloads with the error Profile user must exist and Profile user can't be blank . The form loads fine including the nested models form, but for what ever reason it is failing to save the parent model before attempting to save the child model with the following output to the console: Puma starting in single mode... * Version 3.7.0 (ruby 2.2.6-p396

AJAX update of accepts_nested_attributes_for partials

落爺英雄遲暮 提交于 2020-02-15 22:46:27
问题 My current working environment is Rails 2.3.8 (various reasons why my company hasn't moved to Rails 3). I'm trying to update elements of a multi-model form via AJAX calls - the idea being to replace certain dropdowns depending on how the user selects or fills in other fields. I have previously managed to get this working by using non-form based partials - the problem I have now is to reproduce the AJAX updating of the select dropdowns when the partials are based around form_for and fields_for

AJAX update of accepts_nested_attributes_for partials

不羁的心 提交于 2020-02-15 22:43:39
问题 My current working environment is Rails 2.3.8 (various reasons why my company hasn't moved to Rails 3). I'm trying to update elements of a multi-model form via AJAX calls - the idea being to replace certain dropdowns depending on how the user selects or fills in other fields. I have previously managed to get this working by using non-form based partials - the problem I have now is to reproduce the AJAX updating of the select dropdowns when the partials are based around form_for and fields_for

Rails before_update callback with nested attributes

风格不统一 提交于 2020-02-07 11:23:29
问题 I have two models (lets call then A and B ). A has_many b s and B belongs_to A . class A < ApplicationRecord has_many :bs, dependent: :destroy, inverse_of: :a accepts_nested_attributes_for :bs, reject_if: :all_blank, allow_destroy: true validates_associated :bs end class B < ApplicationRecord belongs_to :a, inverse_of: :bs before_update :do_something, unless: Proc.new { |b| b.a.some_enum_value? if a } def do_something self.some_field = nil end end Other than that, B has a before_update

Rails accepts_nested_attributes_for saves no nested records if one record is invalid

旧城冷巷雨未停 提交于 2020-01-30 06:23:20
问题 Creating a record with nested associations fails to save any of the associated records if one of the records fails validations. class Podcast < ActiveRecord::Base has_many :episodes, inverse_of: :podcast accepts_nested_attributes_for :episodes end class Episode < ActiveRecord::Base belongs_to :podcast, inverse_of: :episodes validates :podcast, :some_attr, presence: true end # Creates a podcast with one episode. case_1 = Podcast.create { title: 'title' episode_attributes: [ {title: "ep1", some

Rails accepts_nested_attributes_for saves no nested records if one record is invalid

耗尽温柔 提交于 2020-01-30 06:21:26
问题 Creating a record with nested associations fails to save any of the associated records if one of the records fails validations. class Podcast < ActiveRecord::Base has_many :episodes, inverse_of: :podcast accepts_nested_attributes_for :episodes end class Episode < ActiveRecord::Base belongs_to :podcast, inverse_of: :episodes validates :podcast, :some_attr, presence: true end # Creates a podcast with one episode. case_1 = Podcast.create { title: 'title' episode_attributes: [ {title: "ep1", some

Rails accepts_nested_attributes_for saves no nested records if one record is invalid

旧街凉风 提交于 2020-01-30 06:20:14
问题 Creating a record with nested associations fails to save any of the associated records if one of the records fails validations. class Podcast < ActiveRecord::Base has_many :episodes, inverse_of: :podcast accepts_nested_attributes_for :episodes end class Episode < ActiveRecord::Base belongs_to :podcast, inverse_of: :episodes validates :podcast, :some_attr, presence: true end # Creates a podcast with one episode. case_1 = Podcast.create { title: 'title' episode_attributes: [ {title: "ep1", some

Devise polymorphic association nested attributes with simple form using rails 4

左心房为你撑大大i 提交于 2020-01-24 20:20:27
问题 I am making a polymorphic association with devise and simple for but for some reason i cant get the params to work here is my code: User: class User < ActiveRecord::Base devise :database_authenticatable, :rememberable, :trackable, :validatable belongs_to :loginable, polymorphic: true end Designer: class Designer < ActiveRecord::Base has_one :user, as: :loginable accepts_nested_attributes_for :user end Layout: <%= simple_form_for [:admin, @designer] , :html => { :class => 'form-horizontal' }

How to get devise to work with accepts_nested_attributes_for in a has one relationship?

﹥>﹥吖頭↗ 提交于 2020-01-23 10:45:08
问题 I am trying to get my user form to also allow the user to fill out their company profile at the same time via form_for. For some reason it is not showing the company fields. Here is my code for the controller and layouts. class User < ActiveRecord::Base attr_accessible :company_attributes has_one :company accepts_nested_attributes_for :company end class Company < ActiveRecord::Base belongs_to :user # Validation validates :name, :presence => true end <%= f.fields_for :company do |company_form|