Cannot create a record with association Active Admin

858 views Asked by At

I dont know why this os causing me such a headache, but it is..I would like a form to create a member who will have an associated member_type attached to it

My Setup so far

class Member < ActiveRecord::Base
 has_one :membership
 accepts_nested_attributes_for :membership
 attr_accessible :membership_id, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email
end

class Membership < ActiveRecord::Base
belongs_to :member
attr_accessible :membership_type
end

Member model has membership_id as the foreign key.

Then my admin/member.rb form, which is where the problems begin.I cant seem to create the member record with the relevant membership_id.

form do |f|
f.inputs "Member Registration" do
  f.input :forename
  f.input :middlename
  f.input :surname
  f.input :house_no
  f.input :house_name
  f.input :street
  f.input :town
  f.input :postcode
  f.input :home_tel
  f.input :mobile_tel
  f.input :work_tel
  f.input :email
end
   f.inputs :for => [:membership, f.object.membership || Membership.new] do |m|
   m.input :membership_type, :label => 'Membership Type', :as => :select, :collection => Membership.all.map{|m| [m.membership_type, m.id]} 
  end 
f.actions
end

all that happens at the moment is the membership model adds another record dependent upon which option has been selected, so Peack Membership is duplicated if selected..

But looking at it now that makes sense as im asking it to create a new Membership record..So how do i get it to create a member record with a membership id

So lost right now

1

There are 1 answers

1
Allerin On BEST ANSWER

Your association is not proper here, As per my understanding I thing you either need to create a new member or need to update a existing member by selecting a MemberShip type.

So, In this case you need to have membership_id in members table.

class Member < ActiveRecord::Base
 belongs_to :membership
 attr_accessible membership_id, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email
end

class Membership < ActiveRecord::Base
  has_many :members
  attr_accessible :membership_type
end

form do |f|
  f.inputs "Member Registration" do
    f.input :forename
    f.input :middlename
    f.input :surname
    f.input :house_no
    f.input :house_name
    f.input :street
    f.input :town
    f.input :postcode
    f.input :home_tel
    f.input :mobile_tel
    f.input :work_tel
    f.input :email
  end
  f.input :membership_id, :label => 'Membership Type', :as => :select, :collection => Membership.all.map{|m| [m.membership_type, m.id]} 
  f.actions
end