find_by_ problem

This works only if one record is matched, but there are two or more records it only prints first of the record. Please help me, i'm just beginner in rails

This is my controller:

def search   @info= Info.find_by_name(params[:desire])   redirect_to :action => 'list2', :id => @info end

And this is my view:

<% for column in Info.content_columns %>     <th><%= column.human_name %></th>   <% end %>   </tr>   <tr>   <% for column in Info.content_columns %>     <td> <%=h @info.send(column.name) %></td>   <% end %>

This works only if one record is matched, but there are two or more records it only prints first of the record. Please help me, i'm just beginner in rails

that's what find_by_xxx does, it boils down to a find :first.
find_all_by_xxx is the find :all equivalent.

Fred

that's what find_by_xxx does, it boils down to a find :first. find_all_by_xxx is the find :all equivalent.

Fred

i changed the code as >> Info.find_all_by_name(params[:desire]) but this time it gives this error:

Routing Error no route found to match "/musteri/list2/#<Info:0x46ada34>/#<Info:0x46ada0c>" with {:method=>:get}

because @info now contains several records

redirect_to :action => 'list2', :id => @info

so @info is an array like set of ActiveRecords @info.id is only an ruby internal object id

redirect_to :action => 'list2', :id => @info.first

should work

Thorsten Mueller wrote:

because @info now contains several records

redirect_to :action => 'list2', :id => @info

so @info is an array like set of ActiveRecords @info.id is only an ruby internal object id

redirect_to :action => 'list2', :id => @info.first

should work

Sorry it doesn't work still it prints the first record. Is there a problem with my view file => list2.rhtml ? I think i must write another for block for loop in array <% for ..............%> ==>>> like this but i really dont know what should i write

<% for column in Info.content_columns %>     <th><%= column.human_name %></th>   <% end %>   </tr>   <tr>   <% for column in Info.content_columns %>     <td> <%=h @info.send(column.name) %></td>   <% end %>

Thorsten Mueller wrote:

because @info now contains several records

redirect_to :action => 'list2', :id => @info

so @info is an array like set of ActiveRecords @info.id is only an ruby internal object id

redirect_to :action => 'list2', :id => @info.first

should work

you'd have to do something like

redirect_to url_for(:action => 'list2') + '?' + [1,2,3].to_query('ids')

Then in your list2 action load the corresponding infos ( params[:ids]
will be the list of ids) and iterate over them in your view. Would be easier if you didn't have the redirect in the middle.

Fred

Please somebody help my problem. Did i ask a difficult or dummy question?

You're being more than a little jumpy if you're posting this 15 minutes after having posted your question.

Fred

It doesn't work

def search    @info= Info.find_all_by_name(params[:desire])    redirect_to url_for(:action => 'list2') + '?' + [1,2,3].to_query('ids')   end

It doesn't work

   def list2     @info = Info.find(params[:ids])   end

  def search     @info= Info.find_all_by_name(params[:desire])     redirect_to url_for(:action => 'list2') + '?' +     [1,2,3].to_query('ids')    end

  <% for column in Info.content_columns %>     <th><%= column.human_name %></th>   <% end %>   </tr>   <tr>   <% for column in Info.content_columns %>     <td> <%=h @info.send(column.name) %></td>   <% end %>

  Offff. I can't believe how could be this thing so hard with rails.

It doesn't work

  def list2    @info = Info.find(params[:ids]) end

def search    @info= Info.find_all_by_name(params[:desire])    redirect_to url_for(:action => 'list2') + '?' +    [1,2,3].to_query('ids')   end

The 1,2,3 was obviously an example. That should be

    @infos= Info.find_all_by_name(params[:desire])     redirect_to url_for(:action => 'list2') + '?' +     @infos.to_query('ids')

<% for column in Info.content_columns %>    <th><%= column.human_name %></th> <% end %> </tr> <tr> <% for column in Info.content_columns %>    <td> <%=h @info.send(column.name) %></td> <% end %>

Offff. I can't believe how could be this thing so hard with rails.

Info.find will return an array if you give it an array of ids. You
need to iterate over that collection. Any entry level rails tutorial
should cover this sort of thing.

Fred

Fred has (as usual) offered very good help. I believe that your problem is that you have poorly modeled routes. A 'list' type of action is generally a collection method, not a member method, and thus has no id associated with it.

It also makes little sense to respond to one request by fetching the necessary information and _always_ redirecting. Perhaps you should be using:

render :action=>'list2'

instead. That way you'd be using the data that you've already gathered (@info) and simply rendering it out using the template from another action.

Long story short, it's not Rails that's the problem, it's having too loose a grasp on its principles.