nested-attributes

accepts_nested_attributes_for causing SQLException

瘦欲@ 提交于 2020-01-23 08:00:27
问题 I would like to use accepts_nested_attributes_for to create an Article object that has_many Sections. class Article < ActiveRecord::Base has_many :sections, :order => "position", :dependent => :destroy belongs_to :categories accepts_nested_attributes_for :sections, :allow_destroy => true, :reject_if => lambda { |attributes| attributes['title'].blank? } validates_presence_of :name, :on => :create, :message => "An article must have a title" end class Section < ActiveRecord::Base belongs_to

How to use nested_attributes when processing JSON?

╄→尐↘猪︶ㄣ 提交于 2020-01-17 12:41:56
问题 I'm trying to write an update method that processes JSON. The JSON looks like this: { "organization": { "id": 1, "nodes": [ { "id": 1, "title": "Hello", "description": "My description." }, { "id": 101, "title": "fdhgh", "description": "My description." } ] } } Organization model: has_many :nodes accepts_nested_attributes_for :nodes, reject_if: :new_record? Organization serializer: attributes :id has_many :nodes Node serializer: attributes :id, :title, :description Update method in the

rails ams, Nested models undefined method `' for nil:NilClass

对着背影说爱祢 提交于 2020-01-17 05:46:46
问题 I have the following models: class Appeal < ActiveRecord::Base belongs_to :applicant, :autosave => true belongs_to :appealer, :autosave => true end class Appealer < ActiveRecord::Base has_many :appeals, :autosave => true end class Applicant < ActiveRecord::Base has_many :appeals end What I want is for every appealer to hold a reference to the applicant of his last appeal so I modified Appealer model to: class Appealer < ActiveRecord::Base has_many :appeals, :autosave => true def last

Ruby on Rails - Nested Attributes and Automatic object creation

孤街醉人 提交于 2020-01-17 05:12:22
问题 I asked a previous question and this one follows... I have 3 Models: Contract, Addendum and Appointment Contract.rb has_many :addendums accepts_nested_attributes_for :addendums Addendum.rb belongs_to :contract has_many :appointments Appointment.rb belongs_to :addendum When an Addendum is created, some Appointments are created automatically. I made this work adding a function on the Addendum's Controller like this def createNewAppointments appointment = Appointment.new(params[:appointment]);

Not able to edit(update nested model rails 4

断了今生、忘了曾经 提交于 2020-01-16 19:25:09
问题 i am building nested form in rails 4.I every thing works as expected But when i try to edit the page then it edit only parent element here project.Child model question elements are not even display in edit mode my _form.html.erb as <%= nested_form_for (@project) do |f| %> <% if @project.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2> <ul> <% @project.errors.full_messages.each do |message| %> <li><%

NoMethodError in As1851Tests#new with shallow routes

。_饼干妹妹 提交于 2020-01-15 09:32:24
问题 I have a rails 5 app using shallow resource nesting. The form_for has stopped working and I can't find why. The App has multiple nested models that worked before changing to shallow routes on the recommendation from my previous question found here My Schema is as follows: db/schema.rb: create_table "as1851_tests", force: :cascade do |t| t.bigint "damper_id" t.boolean "AS1851A", default: false, null: false t.boolean "AS1851B", default: false, null: false t.boolean "AS1851C", default: false,

Rails - I can't display locations on my users form using nested forms

六眼飞鱼酱① 提交于 2020-01-07 07:43:06
问题 I can't display the locations that belogns to the user using nested forms. I want to create the user and save the address, city, state, etc in location model and the name, username, email in the user model. I have no errors on the console. But the fields (select or dropdowns, text_fields, etc) are not showing up. The database has records. This is my user.rb model: class User < ActiveRecord::Base has_many :locations accepts_nested_attributes_for :locations end This is my location.rb model:

Update model nested attributes with composite key in rails

情到浓时终转凉″ 提交于 2020-01-06 17:09:33
问题 I have a model with one_to_many relatioship: class Work < ActiveRecord::Base has_many :work_right_holders accepts_nested_attributes_for :work_right_holders, allow_destroy: true end class WorkRightHolder < ActiveRecord::Base self.primary_keys = :work_id, :right_holder_id, :role belongs_to :work belongs_to :right_holder end When I try to update a work with nested attributes, it create new instances of object in the relationship, instead of updating the existing based on their primary key: work

Routing Error: uninitialized constant for nonexistent controller

╄→гoц情女王★ 提交于 2020-01-06 14:30:47
问题 Upon checking submit I get Routing Error: uninitialized constant DuelersController duels/show.html.erb The loser(s) will <%= @duel.consequence %><br><br> If everyone succeeds they will <%= @duel.reward %> <%= form_for @dueler do |f| %> Accept? <%= f.check_box :accept %> <%= f.submit %> <% end %> There is no DuelersController only DuelsController def show @dueler = Dueler.find_by(user_id: current_user.id) respond_with(@duel) end def set_duel @duel = Duel.find(params[:id]) end Once a user

Ruby on Rails nested attributes not saving into database?

末鹿安然 提交于 2020-01-06 02:54:08
问题 I'm trying to create a list of items within a "Todo list", however, I'm not sure if I'm doing this correctly with nested attributes. I think using a nested attribute is the right attempt because there's going to be a large list of items, and it will be associated with the correct "Todo list" based on ids. Example of what the tables might look like when records are populated Todo table id list 1 grocery shopping 2 health insurance Item table id todo_id name 1 1 buy milk 2 1 buy cereal 3 2 Blue