Hi Chris,
This is almost exactly what I have done. But doesn’t it look backwards?
This is taking the child and associating it to the parent upon child
creation. I know it works, but instead of having to keep track of the
parent id’s I was thinking there is some way for the parent to
instantiate a child record creation and do this for me.
Imagine what my Activity controller new and create actions are going to
look like once I add leads, contacts, accounts, etc… - basically every
parent that an activity can be associated with. I’ll have to check a
bunch of different params to know which one I’m creating the child
against.
If this is the correct way to do it then great. I feel it is backwards
though and that is why I am soliciting this group for advice on best
practices.
Suggestions?
Thanks,
Michael
Chris H. [email protected] wrote:
completely untested and not even sure if this is what you are attempting
to do…but i gave it a go 
class LeadController < ApplicationController
def show
# load the specified lead
@lead = Lead.find(@params[:id])
# in your show.rhtml view, show a link to create a new activity that
points to :controller => “activity”, :action => “new”, :lead_id =>
@lead.id
end
end
class Activity Controller < ApplicationController
def new
# create a new activity to use to generate form data
@activity = Activity.new(:lead_id => @params[:lead_id])
# form in new.rhtml should point to :action => :create
# and should include hidden field with lead id
# hidden_field(:activity, :lead_id ...)
end
def create
# instantiate a new activity loaded with the form data
activity = Activity.new(@params[:activity])
# save the activity
if activity.save
flash[:notice] = “activity created successfully”
else
flash[:error] = “could not save activity”
end
redirect_to :controller => “lead”, :action => “show”, :id =>
activity.lead_id
end
end
On 11/10/05, Michael [email protected] wrote:Hi Nathan,
Thank you for the input. I am failing to grasp a simple concept and it
is frustrating!!! I wish I had a working example so that I could figure
out what I’m missing.
In your example below, you show the “build” and the “create” - but I am
missing how the values are getting populated in that.
Forgive me for a long email, but I want to describe my flow a little
better so that someone can fill in the gap(s). Your input may be very
valid, but since I’m missing something along the way I’m not able to tie
it together.
Parent A:
Child A
Child B
I build a view that shows the details of parent A (show record) and in a
table below that, on the same view, I provide a list of children that
relate to that parent.
Right below the list of Children, I have a button to create a new child
(child c in this case) for that parent.
Parent A controller = Leads
Parent B controller = Activity
When a user invokes the New button, the form currently does this:
start_form_tag
:controller => ‘activity’, :action => ‘new’, :id => @lead
I have tried to change the controller to ‘lead’ and have a new action
along the lines of ‘new_activity’ and then call the build or create but
I don’t know how to get to the controller for the activity from here so
that I can utilize the “new” rhtml file that I have for it. Does that
make sense?
I need to create new activities from many different controllers (leads,
contacts, invoices, etc…). I don’t want to repeat the code across all
of them??? Is my structure wrong? I know for sure I have something
wrong or am missing a very basic concept. If someone knows of a working
sample that has similar flow then that would be great. The example from
the agile book where you have a Cart and Add line items to it isn’t the
same because the line items already exist in the database.
Thanks to anyone that can set me straight!
Michael
nathan [email protected] wrote:
I am sure everyone is just very busy. I am brand new at this and have
not even been lurking on this list very long, but I can tell you how I
do it.
It seems like I should be able to call something like Parent.Child.new
Zactly… except its called Parent.Child.build(attributes = {}) or
Parent.Child.create(attributes = {}).
The build method returns a fresh child object with populated data.
The create method returns the same but already saved in the database.
Took me a while to get this too… even though it is of course basic
and simple. I really like the HowTo wiki, but often I have to study
the api to find what I need.
I think what you need is right here:
http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000464
Like I said, I’m just learning this myself… and spending 12 - 16
h ours a day on it… but its worth it. The only thing I miss about
PHP is the annotated documentation. But the beauty of Ruby and the
Rails framework, makes me happy to spend the extra time to find what I
know is there.
-=nathan
On 11/10/05, Michael wrote:
Your help is appreciated.
Michael
Yahoo! FareChase - Search multiple travel sites in one click.
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
Yahoo! FareChase - Search multiple travel sites in one click.