Skip to content

Instantly share code, notes, and snippets.

@andrewkkirk
Created November 17, 2012 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewkkirk/8581a6ccefb3636cc036 to your computer and use it in GitHub Desktop.
Save andrewkkirk/8581a6ccefb3636cc036 to your computer and use it in GitHub Desktop.
Passing nil into "connection" Object
class Connection < ActiveRecord::Base
attr_accessible :reason, :established, :connector, :connectee1, :connectee2,
:connectee1_attributes, :connectee2_attributes, :connector_attributes
belongs_to :connector, class_name: "User"
belongs_to :connectee1, class_name: "User"
belongs_to :connectee2, class_name: "User"
accepts_nested_attributes_for :connector, :connectee1, :connectee2
validates :reason, :presence => true
validates_length_of :reason, :maximum => 160
after_create :mail_connectees
after_initialize :build_associated_parties
after_create :log_successful_mail
# Builds connectee's and connector objects
def build_associated_parties
build_connector
build_connectee1
build_connectee2
end
# Sends email to both connectee1 and connectee2 with offer to connect #
def mail_connectees
ConnectionMailer.connectee1_email(connectee1).deliver
ConnectionMailer.connectee2_email(connectee2).deliver
end
def log_successful_mail
Rails.logger.info "Mailed successfully!"
end
end
class ConnectionsController < ApplicationController
# instantiate new connection object #
def new
@connection = Connection.new
@connection.build_connectee1
@connection.build_connectee2
@connection.build_connector
end
# save attributes to connection #
def create
debugger
@connection = Connection.new params[:connection]
if @connection.save
flash[:notice] = "Connection created successfully!"
redirect_to @connection
else
render :new
end
end
def edit
@connection = Connection.find(params[:id])
end
def update
end
def show
Connection.find(params[:id])
end
end
<h1>Tell Us 2 People to Introduce</h1>
<div class="errors">
<% if @connection.errors.present? %>
<% @connection.errors.each do |key, message| %>
<p class="error"><b><%= key %></b>: <%= uppercase.message %></p>
<% end %>
<% end %>
</div>
<%= form_for @connection do |f| %>
<h2>Tell us two poeple you want to connect?</h2>
<%= f.fields_for :connectee1 do |c1| %>
<%= c1.label :name1 %>
<%= c1.text_field :name%>
<%= c1.label :email1 %>
<%= c1.email_field :email%>
<% end %>
<%= f.fields_for :connectee2 do |c2| %>
<%= c2.label :name2 %>
<%= c2.text_field :name%>
<%= c2.label :email2 %>
<%= c2.email_field :email%>
<% end %>
<h2>Who should get the credit?</h2>
<%= f.fields_for :connector do |c| %>
<%= c.label :name %>
<%= c.text_field :name%>
<%= c.label :email %>
<%= c.email_field :email%>
<% end %>
<%= f.label :reason %>
<%= f.text_area :reason %>
<%= f.submit "Introduce" %>
<% end %>
@andrewkkirk
Copy link
Author

I'm building the new variables in new method, but values are not getting passed to create method.

@andrewkkirk
Copy link
Author

For some reason when I build the nested attributes for @connection in the new method, the values are not being passed to my create method (i think).

@andrewkkirk
Copy link
Author

the error was from my method

after_initialize :build_associated_parties

it was not executing the following properly

build_connector, build_connectee1, build_connctee2
I moved it to connections_controller and corrected the error. Don't understand why, but it does.

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

@AnkurGel
Copy link

As soon as new method is called, those variables are instantiated and your new page is then rendered. All those variables will be present only in your new scope. When you submit the page/form on that page with some action, your create is called. create will only receive the parameters (params) passed on to it by the page; and hence will not retain variables from new. :) Class variables may help you out here. Looking at code again.

@ajsharp
Copy link

ajsharp commented Nov 17, 2012

Can you paste in or screenshot the error backtrace you're getting?

@ajsharp
Copy link

ajsharp commented Nov 20, 2012

I think I know what was happening here. I think the build_* calls were overwriting the values passed in from the form. I think that method should probably be this:

def build_associated_parties
  build_connectee1 unless connectee1
  build_connectee2 unless connectee2
  build_connector unless connector
end

@andrewkkirk
Copy link
Author

@ajsharp - you were spot on.

Now I need to search through the API docs to find out why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment