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 588075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:16:49+00:00 2026-05-13T15:16:49+00:00

I have a model Journey which has many Users (drivers). I want to be

  • 0

I have a model Journey which has many Users (drivers). I want to be able with help of accepts_nested_attributes_for to add and remove drivers from a journey. When I add a driver I want to show the user a <select> where she can select one of the users to be one of the drivers belonging to that particular journey. I have come that long:

# Models
class Journey < ActiveRecord::Base
  has_many :drivers
  accepts_nested_attributes_for :drivers, :allow_destroy => true
  has_many :users, :through => :drivers
  accepts_nested_attributes_for :users
end

class Driver < ActiveRecord::Base
  belongs_to :journey
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :drivers
  has_many :journeys, :through => :drivers
end

# View _form.html.erb
<% form_for(@journey) do |f| %>
  <%= f.error_messages %>
  <% f.fields_for :drivers do |d| %>
    <%= render :partial => 'driver', :locals => { :f => d } %>
  <% end %>
  <p><%= f.submit 'Submit' %></p>
<% end %>

# View _driver.html.erb
<p><%= f.collection_select(:id, User.all, :id, :name)%></p>

The error says:

ActiveRecord::AssociationTypeMismatch in JourneysController#create
Driver(#2185315860) expected, got Array(#2151950220)

I suspect that my _driver.html.erb is wrong, but I have no idea how to fix it. Could you please help me out with some hints here?

  • 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-13T15:16:49+00:00Added an answer on May 13, 2026 at 3:16 pm

    Your _driver.html.erb should look like this:

    <%= f.collection_select(:user_id, User.all, :id, :name) %>
    

    But I’m not sure if this causes the error.

    Also when I use accepts_nested_attributes_for for nested models, I do it this way:

    # Models
    class Journey < ActiveRecord::Base
      has_many :drivers
      accepts_nested_attributes_for :drivers, :allow_destroy => true
      has_many :users, :through => :drivers
    end
    
    class Driver < ActiveRecord::Base
      belongs_to :journey
      belongs_to :user
      accepts_nested_attributes_for :users
    end
    

    So you can have forms like this:

    <% form_for @journey do |f| %>
       <% fields_for :drivers do |d| %>
          <% fields_for :user do |u| %>
            <%= u.text_field :name %>
            ...
          <% end %>
        <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a IQueryable<Journey> that i collect from my entity model. I want to
I have model Foo which has field bar. The bar field should be unique,
I have model Account, which has n, :transfers . In the list of all
i have model question with text and token fields. Want to add data into
I have model show which has_many performances and a performance has a certain location.
I've users collection with a watchlists field, which have many inner fields too, one
I have model Article it has field title with some text that may contain
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I have a situation where I have Model A that has a variety of
I have model People and model Group, People has field of type Group. How

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.