nested-attributes

Managing child relationships with a nested attribute form

What I'm trying to do is the following: At anyone time a user can have 1 active profile. This active profile must be authorized by an administrator to make sure that it is compliant with the rules and regulations of the site. When a user edits their profile their public profile is unaffected until the administrator signs off their cha...

Remove assosiation instead of destroying object when :allow_destroy => true

When using the new accepts_nested_attributes_for in ActiveRecord, it's possible to use the option :allow_destroy => true. When this option is set, any hash containing nested attributes like {"_delete"=>"1", "id"=>"..."} passed to update_attributes will delete the nested object. Simple setup: class Forum < ActiveRecord::Base has_many ...

Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship

I cannot seem to get a nested form to generate in a rails view for a belongs_to relationship using the new accepts_nested_attributes_for facility of Rails 2.3. I did check out many of the resources available and it looks like my code should be working, but fields_for explodes on me, and I suspect that it has something to do with how I ha...

How access ActiveRecord::AutosaveAssociation.marked_for_destruction? from within parent model

I have a model, Person, with the following association: has_many :distributions accepts_nested_attributes_for :distributions, :allow_destroy => true validate :distributions_must_total_100 The custom validation currently fails when it shouldn't -- when some of the validations have been marked for destruction -- because they still sho...

before_add callback using nested_attributes

Given the following: Foo has_many :bars, :through => :baz and Foo accepts_nested_attributes_for :bar I want to do a find_or_create_by_name when I add a new :bar, but I don't know where I can have some sort of before_add functionality. The background of this question is Bar validates_uniqueness_of :name, which gives errors when I try...

HABTM Double-Nested fields_for

Interesting segment of code that I can't get to work. I have the following models/relationships (unnecessary code excluded) class Service < ActiveRecord::Base belongs_to :service_category, :foreign_key => "cats_uid_fk" belongs_to :service_type, :foreign_key => "types_uid_fk" has_and_belongs_to_many :service_subtypes, :join_table =...

Should it be necessary to call ActiveRecord::Base.reload after updating a nested attribute collection?

I am finding (noodling around in script/console) that if I add a new item to the associated collection, I don't have to call foo.reload to see the resulting change: foo.bars => [] foo.bars_attributes = [{ :person_id => '288', :task_id => '1237' }] => [{ :person_id=>"288", :task_id=>"1237" }] foo.save => true foo.bars => [#<Bar id: 6, pe...

nested attributes and adding attributes through the controller?

As this question is hard to describe, that's the best title i could come up with, so here is some code. Given three Models Parent, Child && Grandchild. Parent < ActiveRecord::Base has_many :children has_many :grandchildren accepts_nested_attributes_for :child end Child < ActiveRecord::Base belongs_to :parent has_many :kids...

Ruby on rails - nested attributes: How to do a find or create of the nested model

I have a Bill model with nested Customer model. The Customer model has a phone number with a uniqueness validation on it. While creating the bill I want to fetch the existing record based on the phone number or create a new one if such doesn't exist. How should I do it in a RESTful way? ...

How to process new children in before_update callback for activerecord nested attributes

I have a model object (let's say Parent) with a has_many association on another model object (Child). class Parent < ActiveRecord::Base has_many :children accepts_nested_attributes_for :children end class Child < ActiveRecord::Base belongs_to :parent end On Parent, I want to add code on the before_update callback to set ...

Ruby on Rails - has_many :through with nested_attributes_for

I'm coming across a problem that I can't find much online for via Google, forums, groups, etc. and so I'm going to raise my hand and ask for help from those who are more wise than I :) I have a rails project setup that was using nested_attributes_for with a one-to-one relationship between two models. It worked quite easily and as expec...

Rails show view of one model with form for adding one child - nested attributes vs seperate controller vs ??

I have a basic two tiered model structure: Articles -> Comments with one Article having many comments. What is the best way to add a "Add a comment" form to the bottom of the Articles show page? nested_attributes is overkill as I don't want to be able to edit all of the comments on the page, just to add one more. Is the best way even ...

accepts_nested_attributes_for with has_many => :through Options

I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model. Associations: class Link < ActiveRecord::Base has_many :tags, :through => :link_tags has_many :link_tags accepts_nested_attributes_for :tags, :allow_destroy => :false, :reject_if => proc { |attrs| attrs.all? { |k...

Form is creating already loaded attributes in addition to new attributes, how do I ignore the first?

In my application you: Have an admin user that signs on and that user has a role (separate model), then I use the declarative_authorization plugin to give access to certain areas. That admin user can also register new users in the system, when they do this (using Authlogic) they fill out a nested form that includes that new users' ro...

problem using accept_nested_attributes with polymorphic relations and inheritance

Having this mix of polymorphism and inhertiance, I encounter some weird behaviour: Migration class CreateCommons < ActiveRecord::Migration def self.up create_table :commons do |t| t.string :name t.string :configurable_type t.integer :configurable_id end create_table :specifics do |t| t.integer :nu...

Rails avoid writing attribute of nested object to log

How can I prevent a certain parameter of a nested relationship in rails from entering the log file - I am writing LARGE files to a column in the db and don't want rails to write that to the log file.. I know of filter_parameter_logging but it doesn't seem to work for nested models - I may just be putting in the wrong spot? ...

How to omit existing child records in a nested form in rails?

In my application an User has many Projects. I want to create a "add many projects" form, so the User can create many Projects at once. It seemed to me that the quickest way was to make a User form with Project fields nested in it, and omit the User fields. This way when the form is submitted, the User is saved and all the new Project r...

Sorting deeply nested attributes in Rails

I want to be able to drag and drag App model which is nested under Category model. http://railscasts.com/episodes/196-nested-model-form-part-1 Here's the Railscast I've tried to follow. #Category controller def move params[:apps].each_with_index do |id, index| Category.last.apps.update(['position=?', index+1], ['id=?', Cate...

accepts_nested_attributes with Model.update for multiple models

Hi, I'm trying to follow http://railscasts.com/episodes/198-edit-multiple-individually but I would like to save objects which are nested (accepts_nested_attributes_for). I've added the following in my controller: def edit_multiple @people = Person.find(params[:person_ids], :include => [:parameters]) end def update_multiple keys =...

Nested attributes in the index view?

I seem to be getting error: uninitialized constant Style::Pic when I'm trying to render a nested object in to the index view the show view is fine. class Style < ActiveRecord::Base #belongs_to :users has_many :style_images, :dependent => :destroy accepts_nested_attributes_for :style_images, :reject_if => proc { |a| a.all? { |k, v| ...