nested-attributes

Rails 4 Not Updating Nested Attributes Via JSON

亡梦爱人 提交于 2019-11-30 01:51:28
问题 I've scoured related questions and still have a problem updating nested attributes in rails 4 through JSON returned from my AngularJS front-end. Question: The code below outlines JSON passed from AngularJS to the Candidate model in my Rails4 app. The Candidate model has many Works, and I'm trying to update the Works model through the Candidate model. For some reason the Works model fails to update, and I'm hoping someone can point out what I'm missing. Thanks for your help. Here's the json in

Nested attribute update_attributes uses insert rather than update

泄露秘密 提交于 2019-11-30 01:30:16
I have a user and nested profile class as follows: class User < ActiveRecord::Base has_one :profile attr_accessible :profile_attributes accepts_nested_attributes_for :profile end class Profile < ActiveRecord::Base belongs_to :user attr_accessible :name end user = User.find(1) user.profile.id # => 1 user.update_attributes(profile_attributes: {name: 'some name'}) user.profile.id # => 2 I don't understand why rails is throwing away the old profile and creating a new one. Using user.profile.update_attributes({name: 'some name'}) just updates the current profile as expected. But in that case I'm

How to maintain the ordering for nested attributes when using accepts_nested_attributes_for in a Rails application

心已入冬 提交于 2019-11-30 00:39:55
问题 Here is the parent model: class TypeWell < ActiveRecord::Base ... has_many :type_well_phases, :dependent => :destroy accepts_nested_attributes_for :type_well_phases, :reject_if => lambda { |a| a[:phase_id].blank? }, :allow_destroy => true ... end Here is the nested model: class TypeWellPhase < ActiveRecord::Base belongs_to :type_well belongs_to :phase end Here is the Phase model: class Phase < ActiveRecord::Base ... has_many :type_well_phases ... end I add nested records in child table

accepts_nested_attributes_for ignore blank values

余生长醉 提交于 2019-11-29 06:04:08
i have class Profile has_many :favorite_books, :dependent => :destroy has_many :favorite_quotes, :dependent => :destroy accepts_nested_attributes_for :favorite_books, :allow_destroy => true accepts_nested_attributes_for :favorite_quotes, :allow_destroy => true end I have a dynamic form where you press '+' to add new textareas for creating new favorites. What i want to do is ignore the blank ones, I find this harder to sort through in the update controller than a non nested attribute. What i have temporarily is a hack in the after_save callback deleting the empty records. Whats the most rails

Nested attribute update_attributes uses insert rather than update

故事扮演 提交于 2019-11-28 22:19:40
问题 I have a user and nested profile class as follows: class User < ActiveRecord::Base has_one :profile attr_accessible :profile_attributes accepts_nested_attributes_for :profile end class Profile < ActiveRecord::Base belongs_to :user attr_accessible :name end user = User.find(1) user.profile.id # => 1 user.update_attributes(profile_attributes: {name: 'some name'}) user.profile.id # => 2 I don't understand why rails is throwing away the old profile and creating a new one. Using user.profile

rails ActiveAdmin nested form has_one accepts_attributes_for formtastic issue

僤鯓⒐⒋嵵緔 提交于 2019-11-28 19:16:43
I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following is a bug, or if there is some way to do it correctly that I am not understanding. I am trying to use a nested model with a has one relationship, so that I can create a page and fill out it's meta data in 1 step. -- (page has_one meta_data, accepts_nested_attributes_for meta_data) Example 1) in this example, when I click new page, meta data section is there but there are no input fields -- also, if I edit the record, it shows up correctly, however the fieldset is duplicated in the second section... and if

Ruby on Rails - nested attributes: How do I access the parent model from child model

荒凉一梦 提交于 2019-11-28 18:47:44
I have a couple of models like so class Bill < ActiveRecord::Base has_many :bill_items belongs_to :store accepts_nested_attributes_for :bill_items end class BillItem <ActiveRecord::Base belongs_to :product belongs_to :bill validate :has_enough_stock def has_enough_stock stock_available = Inventory.product_is(self.product).store_is(self.bill.store).one.quantity errors.add(:quantity, "only #{stock_available} is available") if stock_available < self.quantity end end The above validation so obviously doesn't work because when I'm reading the bill_items from nested attributes inside the bill form,

Rails 3: How does “accepts_nested_attributes_for” work?

北慕城南 提交于 2019-11-28 17:13:38
Consider the following association: class Product < ActiveRecord::Base belongs_to :shop accepts_nested_attributes_for :shop end If params[:product][:shop_attributes] = {"name" => "My Shop"} and I do: @product = Product.new(params[:product]) @product.save a new shop with name "My Shop" is created and assigned to the @product , as expected. However, I can't figure out what happens when shop_attributes contains some id , like: params[:product][:shop_attributes] = {"id" => "20", "name" => "My Shop"} I get the following error: Couldn't find Shop with ID=20 for Product with ID= Question 1 What does

Rails 3.1+ Nested Forms Issue: Can't mass-assign protected attributes

半世苍凉 提交于 2019-11-28 14:24:28
I have a basketball app, where a Roster has many Players, and a Player can be on multiple Rosters.(Reason for many-to-many is for a Player-Roster archive) Roster.rb class Roster < ActiveRecord::Base belongs_to :team has_many :rosterizes has_many :players, :through => :rosterizes accepts_nested_attributes_for :players attr_accessible :jersey_number, :team_id, :class_year, :players end Rosterizes.rb (poorly named I know...) class Rosterize < ActiveRecord::Base belongs_to :player belongs_to :roster attr_accessible :player_id, :roster_id end Player.rb class Player < ActiveRecord::Base has_many

Nested paperclip form with multiple images

喜夏-厌秋 提交于 2019-11-28 12:35:30
I have a one-to-many association between a Banana model and an Image model. In addition, each Banana and Image belong to a User (via a separate association because an Image and its Banana might have different Users). I would like a nested form to create Bananas as well as Images. The kicker is that I don't know how many Images to build (note the multiple attribute). The commented out bit of the form below will create the appropriate amount of Images, but won't complete the associated User reference. Is there a way to accomplish this with fields_for (so the associations are completed) as I've