Avoiding Duplicate Data in DB (for use with Rails)

Posted by ants on Stack Overflow See other posts from Stack Overflow or by ants
Published on 2010-03-04T10:00:57Z Indexed on 2010/03/19 23:21 UTC
Read the original article Hit count: 359

I have five tables that I am trying to get to work nicely together but may need some help.

I have three main tables:

  • accounts
  • members
  • and roles.

With two join tables

  • account_members
  • and account_member_roles.

The accounts and members table are joined by account_members (fk account_id and member_id) table.

The other 2 tables are the problem (roles and account_member_roles).

A member of an account can have more than one role and I have the account_member_roles (fk account_member_id and role_id) table joining the account_members join table and the roles table.

That seems logical but can you have a relationship with a join table? What I'd like to be able to do is when creaeting an account, for instance, I would like @account.save to include the roles and update the account_member_roles table neatly ..... but through the account_members join table.

I've tried .....

accept_nested_attributes_for :members, :account_member_roles

in the account.rb but I get .....

ActiveRecord::HasManyThroughCantAssociateThroughHasManyReflection (Cannot modify association 'Account#account_member_roles' because the source reflection class 'AccountMemberRole' is associated to 'AccountMember' via :has_many.)

upon trying to save a record.

Any advice on how I should approach this?

CIA

-ants

© Stack Overflow or respective owner

Related posts about database-design

Related posts about duplicate-data