0

I have the following models

class GymUser < ActiveRecord::Base
  belongs_to :user 
  belongs_to :gym 
end

class User < ActiveRecord::Base
  has_many :gym_users
  has_one :gym

  attr_accessible :gym_users_attributes, :gym_users
  accepts_nested_attributes_for :gym_users
end

I have a form for a new user, with a nested model gym_user. I want to make sure the user doesn't exist already. This is what I'm trying:

def create_member
  @user = User.new(params[:user])
  @user.generate_password
  @dupe = User.find_all_by_email(@user.email)
  if(@dupe)
    @gym_user = GymUser.new(params[:user][:gym_users_attributes])
    @gym_user.user_id = @dupe.id
  elsif @user.save
    @gym_user = @user.gym_users.order('created_at DESC').first
    @gym = Gym.find(@gym_user.gym_id)
  end
end

I know there's only one nested model here, but I can't figure out how to access those nested parameters.

Here's the form itself

<%= form_for @user, :as => :user, :remote => true, :url => { :controller => 'users', :action => 'create_member'} do |f| %>
    <table border="0" cellpadding="10" cellspacing="0">
        <tr>
            <td colspan="2">
                <%= f.label :name %><br />
                <%= f.text_field :name %>
            </td>
            <td colspan="2">
                <%= f.label :email %><br />
                <%= f.text_field :email %>
            </td>
        </tr>
        <% f.fields_for :gym_users do |builder| %>
            <tr>
                <td>
                    <%= builder.label :role_id, "Role" %><br />
                    <%= builder.collection_select(:role_id, @roles, :id, :name, {:include_blank => true}, {:onchange => "new_member_role_changed()"}) %>
                    <%= builder.hidden_field :gym_id, :value => @gym.id %>
                </td>
                <td>
                    <%= builder.label :item_id, "Membership Level" %><br />
                    <%= builder.collection_select(:item_id, @gym.membership_items, :id, :name, {:include_blank => true}) %>
                </td>
                <td>
                    <%= builder.label :has_monthly_billing, "Recurring Billing?" %><br />
                    <%= builder.radio_button :has_monthly_billing, "1" %>Yes &nbsp;
                    <%= builder.radio_button :has_monthly_billing, "0" %>No
                </td>
                <td>
                    <%= builder.label :billing_date %><br />
                    <%= builder.collection_select(:billing_date, (1..31).to_a, :to_s, :to_s, {:include_blank => true}) %>
                </td>
            </tr>
            <tr>
                <td colspan="4">
                    <%= f.submit %>
                    <a href="javascript:close_new_member()">Cancel</a>
                </td>
            </tr>
        <% end %>
    </table>
<% end %>

3 Answers 3

1

I found the answer. I'm not sure if this is the best way but it works

params[:user][:gym_users_attributes].values.first
0

Your association should be

has_one :gym, :through => gym_users

Also, can you post your form paramaters?

2
  • All I really need to know is how to access those parameters.
    – Jhorra
    Aug 9, 2011 at 20:50
  • they should print out in your development log. also params.inspect in your controller
    – Sameer C
    Aug 11, 2011 at 20:56
0

Your focus is on not allowing creation of duplicate users, and you are using email to verify this. You should rather use the helper

validates_uniqueness_of :email

in your User model. As you have a nested attribute, your object will be created only after Rails validates that a user with same email doesn't exist already.

I hope I understood your problem right.

3
  • Well, rather than tell them the user already exists I want to just take the user and create a new gym_user based on the posted values.
    – Jhorra
    Aug 9, 2011 at 20:55
  • I do have those validations set, I'm doing this to make it easier on the end user.
    – Jhorra
    Aug 9, 2011 at 20:55
  • Oh ok. And I saw that you found the answer too. Good luck! Aug 9, 2011 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.