Topic: find(:all)

i m newbie ...
i want to know the find all methods..that is i want to know how to use it???????

.find(:all)
.find_by_channel_id
.find_by_channel_id(params[:id])
pls explian .....

Re: find(:all)

Model.find(:all) will return all records for a model

Model.find_by_channel_id params[:id] will find the first record with that channel id

.find_by can be used on any of your database columns and return the first hit making that the same as .find(:first, :conditions => ["column_name", [value]).

You can also use .find_all_by_column_name and get and array of all results making that call essentially the same as find(:all, :conditions => ["column_name = ?", value]).

Hope that helps.

Re: find(:all)

Model.find_by_channel_id params[:id] will find the first record with that channel id.. u said ok..
but if i want to get all the records with that channel id ?????????wat should i do?..
pls help me

Re: find(:all)

The answer lies in my previous post. find_all_by_column_name, in this case the column name is channel_id.

Re: find(:all)

if params[:id ]
         
         
          @cs = ChannelStories.find_all_by_channel_id( params[:id ]



am i right here? i m newbie so i m struggling to understand sorry yar....

Re: find(:all)

Yeah but add a ) to the end. Give stuff like that a bash, you'll learn more by playing with it.

Re: find(:all)

sure...and i thank u for the timely reply
and i want to know  one more thing also..


if params[:id ]
         
         
          @cs = ChannelStories.find_all_by_channel_id( params[:id ]
end
 

in this code @cs contains all the records ..in that  if i want to fetch story_id of all the records from this
    @cs = ChannelStories.find_all_by_channel_id( params[:id ]

wat i do?

Re: find(:all)

You can loop through the array and access the story_id method on each of the objects.

Re: find(:all)

if params[:id ]
         
         
          @cs = ChannelStories.find_all_by_channel_id( params[:id ] )

          @cs.each do |f|
              f.story_id             
             
          end
         
         
end

thank u because of ur help i m getting on with rails ..thank u..

and i have table called stories in that i have fields like
id,
title,
description...
using that story_id i need to get all the fields from the stories table..
i did association between stories table and channel_stories table ...pls help

Re: find(:all)

Ok it seems that you need a deal of help of direction. I would recommend that you go to www.pragprog.com and buy Agile web development on Rails, it will have much more comprehensive help for you than you can get being spoon fed an application on the message board.

Re: find(:all)

surely...i 'll do that
but the problem is i get only one records from the stories when i run my application..why i get as such..what is the reason behind this...
def stories_list
     
      if params[:id ]
         
         
          @cs = ChannelStories.find_all_by_channel_id( params[:id ] )
          @cs.each do |f|
              f.story_id
              @stories = Story.find_all_by_id(f.story_id)
             
               
          end
     
       end
     
end

Re: find(:all)

jesus, I almost don't wanna help you since you are so boring with openng simmilar threads all the time...

@cs = ChannelStories.find_all_by_channel_id( params[:id ] )

If I understand correctly you probably don't have an array in params[:id] but a single id.
So @cs variable has only one channelstory. So now you understand why you have only one record.
Now please stop opening the same threads !

Re: find(:all)

Also, the AR find syntax has been updated to support:

User.all

or
User.first

To return either all User objects or the first, respectively.  This also applies to "last" and other handy methods.

I would also check out the following Railscast for a great introduction to dynamic find by methods:
http://railscasts.com/episodes/2-dynami … by-methods

You should also take a look at named_scope which will help clean things up a bit for you as well:
http://railscasts.com/episodes/108-named-scope

Good luck!

Re: find(:all)

User.all, User.first .. aren't being used for quite a while and were raplaced by:

User.find(:all), User.find(:first) etc.

Re: find(:all)

Eric_Jardas wrote:

User.all, User.first .. aren't being used for quite a while and were raplaced by:

User.find(:all), User.find(:first) etc.

User.all etc etc was just recently added into Rails (well, it was introduced in Rails 2.x) as an alias (not necessarily to replace) for the User.find(:all) methods.  (http://ryandaigle.com/articles/2008/3/2 … ctionality)

So I would consider the User.all method newer, easier to understand and a quicker solution (as in less to type, I don't know how it would compare in a benchmark against User.find(:all), though I think the results would be very close).

Re: find(:all)

those are custom scopes.
But you are right, you can use Model.all and so on..didn't knew that nor seen anyone use it.

Last edited by Eric_Jardas (2008-08-11 11:10:26)

Re: find(:all)

if params[:id ]
         
         
          @cs = ChannelStories.find_all_by_channel_id( params[:id ] )
          @cs.each do |f|
              f.story_id
              @stories = Story.find_all_by_id(f.story_id)
             
               
          end
     
       end
     
end
ok @stories = Story.find_all_by_id(f.story_id) in this code can use array to store the story_id ..?how can i implement that?????/ can you help me...

Last edited by Rethy (2008-08-11 23:11:30)