ruby on rails - ActiveAdmin has_many form not saved if parent model is new and is NOT NULL in child model -


i have 2 models, room , student. room has_many students. student belongs_to room.

i got error room can't blank when try add student room during creating new room.

my guess that, upon submission, child object (student) saved before parent object (room) saved. there way bypass order without remove not null setting on room_id? or guess wrong? or worse, doing wrong?

# app/models/room.rb class room < activerecord::base   validates :name, presence: true   has_many :students    accepts_nested_attributes_for :students end    # app/models/student.rb class student < activerecord::base   validates :name, presence: true    belongs_to :room   validates :room, presence: true # room_id set not null in database too.  end    # app/admin/room.rb   form |f|     f.semantic_errors *f.object.errors.keys     f.inputs "room details"       f.input :name        f.has_many :students |student|         student.input :name       end     end      f.actions   end    permit_params :name, students_attributes: [:name] 

rails needs made aware how belongs_to , has_many relate each other. filling has_many , testing belongs_to have "explain" rails associations inverses of each other :)

so in case should trick:

class room < activerecord::base   has_many :students, :inverse_of => :room   accepts_nested_attributes_for :students end  class student < activerecord::base   belongs_to :room, :inverse_of => :students   validates_presence_of :room end  

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -