Richlewis
Richlewis

Reputation: 15394

Association Clarification Rails

This is probably so simple its stupid, but I'm having a brainfreeze moment and am just staring at the screen now going nowhere.

I have two models a member and a membership, each member can have one type of membership from a selection of many.

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

class Membership < ActiveRecord::Base
 attr_accessible :membership_type
end

My membership model will have a few records pre populated so that a member can choose which type of membership they would like, ie Peak, Off Peak, Student

Am i correct in thinking that the member model will look like this

class Member < ActiveRecord::Base
 **has_one :membership**(added this)
 **accepts_nested_attributes_for :membership**
 attr_accessible **:membership_attributes(Added This)**, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email   

end

So i create a migration and add the membership_id column to the member model as the foreign key?

My Membership Model can look like this

class Membership < ActiveRecord::Base
**belongs_to :member** (Added This)
attr_accessible :membership_type    
end

am i looking at this correctly here?

Thanks

Upvotes: 0

Views: 15

Answers (1)

Pluvio
Pluvio

Reputation: 1598

So i create a migration and add the membership_id column as the foreign key?

I think that in your migration you have to add a member_id column to the memberships table, as the foreign key.

(Active Record Associations has one)

Upvotes: 1

Related Questions