Topic: find not working

I have a small system started. Consists of two MySQL tables; contacts (with an id) and attempts (with an id and contact_id and a foreign key to contacts based on contact_id). Models are: Contact has_many :attempts; Attempt belongs_to :contact.

Models and Controllers and Views were created from command line through scaffold.

In the parent table's show view I've placed links to the child table's controller show method under which the find is "@attempts = Attempt.find(:all, :conditions => ["contact_id = ?",
session[:contactid]]).....(session.contacid was assigned in the parent table's controller show method).

The child tables show view code was replaced with the normal "list" code. This works.

In the scaffold script generated controller for the child table (attempts) the list method uses the following:
       @attempt_pages, @attempts = paginate :attempts, :per_page => 10
which is, of course, different than the find I used under the show method.

My problem is editing the attempts records reflected in the show view (you indeed can edit from the list view). I haven't changed any of the edit code.

When edit is called from show, you can tell from the url that the appropriate id is passed. But by the time the form is displayed it does not have the record. I can't tell where it looses the record id. There is a find in the attempts_controller edit method (@attempt = Attempt.find(params[:id])) but edit.rhtml calls update assigning @attempt to :id. Then there is another find in attempts_controller update method (same as in edit).

I know this may be a terse listing of the problem but I'll provide anything else needed.

Please help, most all my hair is now gone.

thanks

Re: find not working

I read your post a couple times but am having trouble understanding the problem - I'm a little dense today. Would you mind explaining exactly what is wrong? Is the record information not showing up in the edit fields? Take a look in the development log to see what parameters are being passed to the edit action.

Also, post some code, it's hard to see what is going on without it.

Railscasts - Free Ruby on Rails Screencasts

Re: find not working

hi ryanb

thank goodness you always answer me.

I looked in the log and "processing AttemptsController#Edit" with a "Get" apparently indeed
passes the  Parameters: {"action"=>"Edit", "id"=>"3", "controller"=>"attempts"}. So the find should work.

Sorry about the lack of code, so here goes. I think you would need the AttemptsController:

/////////////////////////////////////////////////////////////////

class AttemptsController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @attempt_pages, @attempts = paginate :attempts, :per_page => 10
  end
 
  def show
    @attempts = Attempt.find(:all, :conditions => ["contact_id = ?", session[:contactid]])
#    render :text => session[:contactid]
#    render :text => session[:company]
  end

######### new does NOT show the contact company or id (from session) but does work and
######### properly assigns the contact_id to the new child record in attempts
  def new
    @attempt = Attempt.new(:contact_id => params[session[:contactid]])
#    render :text => session[:company]
  end

  def create
    @attempt = Attempt.new(params[:attempt])
    @attempt.contact_id = session[:contactid]
    if @attempt.save
      flash[:notice] = 'Attempt was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @attempt = Attempt.find(params[:id])
  end

  def update
    @attempt = Attempt.find(params[:id])
    if @attempt.update_attributes(params[:attempt])
      flash[:notice] = 'Attempt was successfully updated.'
      redirect_to :action => 'list', :id => @attempt
    else
      render :action => 'edit'
    end
  end

  def destroy
    Attempt.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end

///////////////////////////////////////////////

You might also need the Attempts/Show.rhtml:
/////////////
<h1>Attempts for This Contact</h1>
<table>
<tr>
<th align=left width = "15%">Contact</th>
<% for column in Attempt.content_columns %>
  <th><%= column.human_name %></th>
<% end %>
</tr>

<% for attempt in @attempts %>
<tr>
  <td width = "15%"><%=session[:company]%></td>
  <% for column in Attempt.content_columns %>
      <td><%=h attempt.send(column.name) %></td>
  <% end %> 
  <td><%= link_to 'Edit', :action => 'Edit', :id => attempt %></td>
  <td><%= link_to 'Delete', {:action => 'destroy', :id => attempt}, :confirm => 'Are you sure?', :post => true %></td>
</tr>
<% end %>
</table>
<%= link_to 'Back', :action => 'list' %>

//////////////////////////////////////////////////////////////////

, and the Attempts/Edit.rhtml

////////////////
<h1>Editing attempt</h1>

<%= start_form_tag :action => 'update', :id => @attempt %>
  <%= render :partial => 'form' %>
  <%= submit_tag 'Update' %>
<%= end_form_tag %>

<%= link_to 'Back', :action => 'show' %>

////////////////////////

and to answer you question; yes, the record information is not showing up in the edit fields.

Again, your help is greatly appreciated........

Re: find not working

Your right, according to the log the id is getting passed, so the @attempt model should be set in the edit action. I'll need one more code from you which I think is where the problem is. I need the /views/attempts/_form.rhtml partial code.

Railscasts - Free Ruby on Rails Screencasts

Re: find not working

you got it..............

the first label was something I was trying to set up so that a user could tell what contact's
ateempt they were editing. That's when I wrote about searching Rails Documentation to find out what the parameteres to text_field were (did I thank you for pointing me to a good site??). It
of course doesn't work. If that one paragraph is my entire problem I might shoot myself.


<%= error_messages_for 'attempt' %>

<!--[form:attempt]-->

<p><label for="contact_company">Contact</label><br/>
<%= text_field 'contact', session[:company] %></p>

<p><label for="attempt_attempt_medium">Attempted By (Phone, Letter)</label><br/>
<%= text_field 'attempt', 'attempt_medium' %></p>

<p><label for="attempt_attempt_date">Attempt date</label><br/>
<%= datetime_select 'attempt', 'attempt_date'  %></p>

<p><label for="attempt_notes">Notes</label><br/>
<%= text_area 'attempt', 'notes'  %></p>
<!--[eoform:attempt]-->

Re: find not working

I'm not sure what you are attempting to do with the contact edit field. Do you want to just display information about the contact who's attempt you are editing? No need to use a text field unless you are editing something

<%= @attempt.contact.name %>

You may need to rename "name" here to some column in the contacts table you want to display.

Railscasts - Free Ruby on Rails Screencasts

Re: find not working

I just want to display the contact's Company name.

For some reason, I can not get the child table's records by just using the @contact.attempts..(as you can tell by my using session variables to tie the tables together) but I hadn't tried the rails magic of going back up from the child to the parent table.

I'm beginning to wonder if I need to install some additional libraries in Rails. I believe I have the latest ActiveRecord (db) libraries prior to 1.2.

Well, I tried this in _form:

<!--[form:attempt]-->

<p><label for="contact_company">Contact</label><br/>
<label for="company"><%= @attempt.contact.company %></p>
....
....

I got an error:

"
You have a nil object when you didn't expect it!
The error occured while evaluating nil.company
"

... which is again indicating to me that the edit method did not find the record.

The log is absolutely full of stuff referencing that error.

I don't know where you are, but I've been here since 6:00AM and its practically 7:30 PM so I'm cutting this machine off and heading for Margaritaville. Maybe if I pick this up with a fresh mind............

Thanks for the help, again. And the banter. I'm having difficulty changing my "style" in order to work through Rails' magical data layer. Please put up with me a bit longer.

Re: find not working

Make sure the associations are set in the Contact and Attempt models:

class Contact < ActiveRecord::Base
  has_many :attempts
end

class Attempt < ActiveRecord::Base
  belongs_to :contact
end


You should then be able to call @contact.attempts and @attempt.contact without problem. Just make sure the contact_id column is set in the database for the given attempt or else it will return nil (which is apparently what is happening here).

It's often useful to open up script/console at a time like this and play around with the models to make sure you understand how the methods work:

ruby script/console

You can input ruby code here and see the results instantly:


contact = Contact.find(:first)
contact.attempts

attempt = Attempt.find(:first)
attempt.contact

Railscasts - Free Ruby on Rails Screencasts

Re: find not working

well, all that worked. I'm researching and experimenting........

Re: find not working

hi;

I have a real problem now.

The app works.

Suffice it to say that I spent a good deal of time researching, reading rails source code,
and trying different things over several days.

I could not get past the find in Attempts returning a nil object (I'm not going to go into all the details of addressing arrays & everything).

Ultimately, I just started following the progress of the app by using flash. I replaced the find in Edit with a conditional statement:
from:
    @attempt = Attempt.find(params[:id])
to:
    if @attempt = Attempt.find(params[:id])
      flash[:notice] = 'Attempt Record found in Controller.edit'
    else
      flash[:notice] = 'Attempt NOT found in edit'
    end

I ran the app and it worked. It readily found any record I wanted.



I have since reverted the app to the original code I sent to you in all files.

The app works.

What could possibly have caused this?????

Getting here involved effort over several days during which the machine was actually physically turned off. Certainly the web server was off many times. And several times I would re-boot the web server (WEBrick - development) and close & restart FireFox and clear its cache in order to get the latest revisions although the documentation indicates you don't have to do this in development mode.

Although we don't have a huge application server with lots of plug-ins running in the background, Rails is essentially fulfilling this role. It's a lot more than just a framework and ORM facility.

I don't know where to start looking to get to the bottom of the problem. I've been thinking about object serialization, looking at object id's, and I can't find an issue.

Any insight???? Please.

Re: find not working

You mentioned it is working now, is there still a problem? Or are you trying to figure out what happened to make it start working?

Railscasts - Free Ruby on Rails Screencasts