Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6361247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:45:07+00:00 2026-05-24T23:45:07+00:00

I have a many-to-many relation setup for a data model which deals with a

  • 0

I have a many-to-many relation setup for a data model which deals with a problem domain of Members and Addresses. Basically a ‘member’ can have multiple ‘addresses’, think of like when you have a work address and a home address.

I want to store additional attributes on the join table and save data, apart from the foreign key attributes. There are many examples on the net about many-to-many relations, but I’ve not found details/examples on storing the extra data.

Here is the problem that i’m stuck with.

The models are setup as:

Models

class Member < ActiveRecord::Base
   has_many :member_addresses
   has_many :addresses, :through => :member_addresses
end

class MemberAddress < ActiveRecord::Base
   belongs_to :address   # foreign key for address -> address_id
   belongs_to :member    # foreign key for member ->member_id
end

class Address < ActiveRecord::Base
   has_many :member_addresses
   has_many :members, :through => :member_addresses

   accepts_nested_attributes_for :members, :allow_destroy => true
end

The ‘accepts_nested_attributes’ on the Address class is for the nested-forms.

Database Schema/Migrations
The tables (simplified for posting) are defined as

create_table "addresses", :force => true do |t|
    t.string  "firstline"
    t.string  "state"
    ....
end

create_table "member", :force => true do |t|
    t.string "name"
    ....
end

create_table "member_addresses", :force => true do |t|
   t.integer "member_id"
   t.integer "address_id"
   t.string  "address_type"
end

The field ‘address_type’ on the join table is intended for flagging the type of the address that is associated with the member (aka; “home” or “work”)

It is updating this field that is where I’m getting stuck.

Controllers & View

The member_controller setups the model like:

members_controller.rb
def new
   @member = Member.new
   @member.addresses.build    #pre-build the associated address
end

Then the view for the member page new action is

<%= form_for @member do |m| %>

  <%= m.label :first_name, "First Name"%>
  <%= m.text_field :first_name %>
  </p>

  <%= m.fields_for :addresses do |addrform|%>
    <%= addrform.label :firstline %>
    <%= addrform.text_field :firstline %>
  <% end %>

  <br />
  <%= label_tag 'Type of address' %>
  <%= text_field_tag 'addrtype'    %>
  <br />
  <%= m.submit "Add" %><br />

<% end %>

The idea is that with the ‘accepts_nested_attributes’ on the Member model I can enter the data on the form to add in the name and address attributes. The additional label_tag is for the data to be posted for the address type that I want to store

So when the form is filled in the post data (form a trap) looks like:

{"utf8"=>"✓",
     "authenticity_token"=>"E0R038rcdJmBGjjxQ9nQLHGVbzM4ejA0vsEaIvqkwkE=",
     "member"=>{"first_name"=>"James",
     "last_name"=>"SMith",
     "addresses_attributes"=>{"0"=>{"firstline"=>"this is the first line"}}},
     "addrtype"=>"home",
     "commit"=>"Add"}

All good – i’m getting my Member data and the Address data, as well as the additional field ‘addrtype’ which is to be used for carrying my additional data

Now how to get the data into the relation or join table ? On the member_controller create action

def create
   # create a member with the params posted 
   @member = Member.new(params[:member])

   # get data for 'relation data'
   @addr_type = params[:addrtype]



  if @member.save!
      #update the relation now created with the address type
      @member.member_addresses.first.address_type = @addr_type

      # save the relationship (is this necessary?)
      @member.save!

      redirect_to members_path
  end

As you can see in the create I’m attempting to fill in the data on the join table and then force a save again so that the relation or association with the additional attribute is saved.

This doesn’t work, and/or I’m doing it wrong.

Any suggestions would be most appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:45:08+00:00Added an answer on May 24, 2026 at 11:45 pm

    Try changing your models to something like this:

    class Member < ActiveRecord::Base
       has_many :member_addresses
       accepts_nested_attributes_for :member_addresses, :allow_destroy => true
    end
    
    class MemberAddress < ActiveRecord::Base
       belongs_to :member    
       has_many :addresses
       accepts_nested_attributes_for :addresses, allow_destroy => true
    end
    
    class Address < ActiveRecord::Base
       belongs_to :member_addresses
    end
    

    And change your member_controller.create method to

    @member = Member.new(params[:member])
    

    This should allow you to create MemberAddresses and Addresses via the MemberController.create method.

    These 2 RailsCasts might also be worth a look

    #196 Nested Model Form Part 1
    #197 Nested Model Form Part 2

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got to classes Product and Store which have many to many relation I
I have 2 tables which have many-to-many relation. Code of entities public class Product
when i have a many-to.many relation with nhibernate and let nhibernate generate my db
I have 2 tables having many to many relation and hence there is junction
I have a question about Criteria method, one-to-many relation to the database, 'one' is
Lets say I have two tables - child and parent with many-to-one relation. What
I have a product table that has a many-to-many relation to itself (using a
I'm a bit struggling with the setup of my models. I have companies which
I have a many-to-many relation in 3 tables: ProgramUserGroup and Feature are the two
I have objects A witch has many to many relation ship to object b.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.