nested-attributes

Nested attributes for one-to-one relation

≡放荡痞女 提交于 2019-12-25 02:18:51
问题 I have a Company which has one Subscription. Now I want a form to add or edit the company and the subscription, so I use "accepts_nested_attributes_for". This is (part of) the Company model: has_one :subscription, :dependent => :destroy accepts_nested_attributes_for :subscription This is (part of) the Subscription model: belongs_to :company In the controller I have this: def new @company = Company.new(:subscription => [Subscription.new]) end def create @company = Company.new(params[:company])

ActiveRecord::AssociationTypeMismatch (dynamic image uploader)

喜欢而已 提交于 2019-12-25 01:39:10
问题 I am trying to follow the tutorial Dynamic multiple image uploads with Ruby on Rails that creates a new model for photos and will associate them with another model. After copying and pasting all the code I'm getting an "AssociationTypeMismatch" error as follows. ActiveRecord::AssociationTypeMismatch in AdminForksController#update Photo(#2176152540) expected, got Array(#2148417160) app/controllers/admin_forks_controller.rb:23:in `update' { "commit"=>"update", "fork"=>{"position"=>"", "name"=>

How do I update a nested resource without passing the data via a form

拟墨画扇 提交于 2019-12-25 01:18:59
问题 Models class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :roles, :dependent => :destroy, :inverse_of => :user has_many :companies, :through => :roles accepts_nested_attributes_for :roles, :limit => 1, :allow_destroy => true end class Role < ActiveRecord::Base belongs_to :user, :inverse_of => :roles belongs_to :company, :inverse_of => :roles accepts_nested_attributes_for :company end class Company <

nested attributes not saving the user_id in join table

跟風遠走 提交于 2019-12-25 00:32:35
问题 Admin Main controller: def new @admin = Admin.new @invitation = @admin.invitations.build @admi.user_admin_relationships.build end def create params[:invitation][:sender_id] = current_user.id @admin = Admin.new(params[:admin]) if @admin.save redirect_to root_path else render 'new' end end Invitation Model belongs_to :admin belongs_to :user Admin Model has_many :invitations has_many :user_admin_relationships has_many :users, :through => :user_admin_relationships accepts_nested_attributes_for

Rails 4, retrieved many records with Model.where(related_model_id: 1), how to show each one in an individual view?

不羁岁月 提交于 2019-12-24 21:39:51
问题 Ok so I'm working on a quiz as part of my app. I have three nested models Quiz, Question, Answer. class Quiz < ActiveRecord::Base belongs_to :video has_many :questions has_many :answers, through: :questions accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, allow_destroy: true end class Question < ActiveRecord::Base belongs_to :quiz has_many :answers accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, allow_destroy:

How to use nested attributes with belongs_to association on new action?

一笑奈何 提交于 2019-12-24 17:55:55
问题 On the update action the following nested_form works, but on create I get this error: Couldn't find Student with ID=12 for Cv with ID= Controller: def new @cv = Cv.new @cv.student = current_student end def create @cv = Cv.new(params[:cv]) if @cv.save redirect_to student_dashboard_path, notice: t('activerecord.successful.messages.created', model: @cv.class.model_name.human) else render 'new' end end Model: class Cv < ActiveRecord::Base attr_accessible :student_attributes belongs_to :student

How to use nested attributes with belongs_to association on new action?

廉价感情. 提交于 2019-12-24 17:55:00
问题 On the update action the following nested_form works, but on create I get this error: Couldn't find Student with ID=12 for Cv with ID= Controller: def new @cv = Cv.new @cv.student = current_student end def create @cv = Cv.new(params[:cv]) if @cv.save redirect_to student_dashboard_path, notice: t('activerecord.successful.messages.created', model: @cv.class.model_name.human) else render 'new' end end Model: class Cv < ActiveRecord::Base attr_accessible :student_attributes belongs_to :student

Undefined Method for ActiveRecord::Relation (Nested Attributes)

我是研究僧i 提交于 2019-12-24 12:03:03
问题 undefined method `results' for Result::ActiveRecord_Relation:0x007f9ee69ad148> Quantifieds#Index The first line here is what causes the exception. <% @quantifieds.results.each do |result| %> <%= result.date_value.strftime("%m-%Y") %> <%= result.result_value %> <% end %> To restate the question, how do I define date_value & result_value so it works in the quantifieds index? They are derived from :results in the _form. I am using cocoon to create the nested attributes. Entire Index (Updated

Using has_one and belongs_to together

家住魔仙堡 提交于 2019-12-24 09:56:53
问题 Is it acceptable to use both has_one and belongs_to for the same link in the same model? Here is how it looks Class Foo has_many :bars has_one :special_bar, :class_name => "Bar" accepts_nested_attributes_for :special_bar, :allow_destroy => true belongs_to :special_bar, class_name => "Bar" end Class Bar belongs_to :foo end The schema is as follows: Foo name special_bar_id Bar name foo_id While this works with the accepts_nested_attributes_for, its using BOTH has_one and belongs_to to achieve

Accessing a join-model attribute stored in a join table created with #create_join_table

≯℡__Kan透↙ 提交于 2019-12-24 04:47:09
问题 In a Rails ( 4.1.5 / ruby 2.0.0p481 / win64 ) application I have a many-to-many relationship between Student and Course and a join model StudentCourse which represents the association, which has an additional attribute called "started", which is set by default on "false". I also have added an index in the join table made of the student_id and the course_id, and set a unique check on that, like this t.index [:student_id, :course_id], :unique => true, :name => 'by_student_and_course' Now I see