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

  • Home
  • SEARCH
  • 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 3783258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:11:57+00:00 2026-05-19T11:11:57+00:00

I’m trying to get it to work but it dosen’t! I have class User

  • 0

I’m trying to get it to work but it dosen’t!

I have

class User < ActiveRecord::Base
  has_many :events, :through => :event_users
  has_many :event_users
  accepts_nested_attributes_for :event_users
end

class Event < ActiveRecord::Base
  has_many :event_users
  has_many :users, :through => :event_users
  accepts_nested_attributes_for :users
end

class EventUser < ActiveRecord::Base
  set_table_name :events_users
  belongs_to :event
  belongs_to :user
  accepts_nested_attributes_for :events
  accepts_nested_attributes_for :users
end

And also the table-layout

event_users
  user_id
  event_id
  user_type
events
  id
  name
users
  id
  name

And this is my form

<%= semantic_form_for @event do |f| %>
  <%= f.semantic_fields_for :users, f.object.users do |f1| %>
    <%= f1.text_field :name, "Name" %>
    <%= f1.semantic_fields_for :event_users do |f2| %>
      <%= f2.hidden_field :user_type, :value => 'participating' %>
    <% end %>
  <% end %>
<%= link_to_add_association 'add task', f, :users %>
<% end %>

The problem is that if I create a new user this way, it doesn’t set the value of user_type (but it creates a user and a event_users with user_id and event_id). If I go back to the edit-form after the creation of a user and submit, then the value of user_type is set in events_users. (I have also tried without formtastic)
Any suggestions? Thanks!

—-edit—-

I have also tried to have the event_users before users

<%= semantic_form_for @event do |f| %>
  <%= f.semantic_fields_for :event_users do |f1| %>
    <%= f1.hidden_field :user_type, :value => 'participating' %>
    <%= f1.semantic_fields_for :users do |f2| %>
      <%= f2.text_field :name, "Name" %>
    <% end %>
  <% end %>
<%= link_to_add_association 'add task', f, :event_users %>
<% end %>

but then it only throws me an error:

User(#2366531740) expected, got
ActiveSupport::HashWithIndifferentAccess(#2164210940)

–edit–

the link_to_association is a formtastic-cocoon method (https://github.com/nathanvda/formtastic-cocoon) but I have tried to do other approaches but with the same result

—edit—-

def create
  @event = Event.new(params[:event])
  respond_to do |format|
    if @event.save
      format.html { redirect_to(@event, :notice => 'Event was successfully created.') }
      format.xml  { render :xml => @event, :status => :created, :location => @event }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }                 
    end
  end
end
  • 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-19T11:11:57+00:00Added an answer on May 19, 2026 at 11:11 am

    To be honest, i have never tried to edit or create a has_many :through in that way.

    It took a little while, and had to fix the js inside formtastic_cocoon to get it working, so here is a working solution.

    You need to specift the EventUser model, and then fill the User model (the other way round will never work).

    So inside the models you write:

    class Event < ActiveRecord::Base
      has_many :event_users
      has_many :users, :through => :event_users
      accepts_nested_attributes_for :users, :reject_if => proc {|attributes| attributes[:name].blank? }, :allow_destroy => true
      accepts_nested_attributes_for :event_users, :reject_if => proc {|attributes| attributes[:user_attributes][:name].blank? }, :allow_destroy => true
    end
    
    class EventUser < ActiveRecord::Base
      belongs_to :user
      belongs_to :event
      accepts_nested_attributes_for :user
    end
    
    class User < ActiveRecord::Base
      has_many :events, :through => :event_users
      has_many :event_users
    end
    

    Then the views. Start with the events/_form.html.haml

    = semantic_form_for @event do |f|
      - f.inputs do
        = f.input :name
    
        %h3 Users (with user-type)
        #users_with_usertype
          = f.semantic_fields_for :event_users do |event_user|
            = render 'event_user_fields', :f => event_user
          .links
            = link_to_add_association 'add user with usertype', f, :event_users
    
        .actions
          = f.submit 'Save'
    

    (i ignore errors for now)
    Then, you will need to specify the partial _event_user_fields.html.haml partial (here comes a little bit of magic) :

    .nested-fields
      = f.inputs do
        = f.input :user_type, :as => :hidden, :value => 'participating'
        - if f.object.new_record?
          - f.object.build_user
        = f.fields_for(:user, f.object.user, :child_index => "new_user") do |builder|
          = render("user_fields", :f => builder, :dynamic => true)
    

    and to end the _user_fields partial (which does not really have to be a partial)

    .nested-fields
      = f.inputs do
        = f.input :name
    

    This should work.
    Do note that i had to update the formtastic_cocoon gem, so you will need to update to version 0.0.2.

    Now it would be easily possible to select the user_type from a simple dropdown, instead of a hidden field, e.g. use

    = f.input :user_type, :as => :select, :collection => ["Participator", "Organizer", "Sponsor"]
    

    Some thoughts (now i proved it works):

    • this will always create new users on the fly, actually eliminating the need for the EventUser. Will you allow selecting existing users from a dropdown too?
    • personally i would turn it around: let users assign themselves to an event!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I need to clean up various Word 'smart' characters in user input, including but
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.