Dariusz S
2005-Apr-08  20:40 UTC
problem with passing a parameter to a condition (for find_all) - newbie question
Hi, 
I''m totally new to rails. I''m trying to populate data from
mysql. I''ve
got a problem when it comes to passing a parameter in a condition (for 
find_all). Definitions in a controller are as follow: 
def list 
    @cf_categories = CfCategory.find_all  "cParent =
''0''"
end 
def subcat 
        @subcats = CfCategory.find_all cParent = @params["id"] 
end 
and I don''t know how to declare the condition in the second definition 
(cParent = @params["id"]). I''ve also checked that the id is
being
passed from the list def to the subcat. 
Can anybody help me? 
Thanks.
Tomas Jogin
2005-Apr-08  22:37 UTC
Re: problem with passing a parameter to a condition (for find_all) - newbie question
I think this is what you''re looking for:
def subcat
        @subcats = CfCategory.find_all "cParent =
#{@params["id"]}"
end
But, allow me to suggest:
def subcat(parent_cat)
        CfCategory.find_all "cParent = #{parent_cat}"
end
Or, if the subcat method is a member of the CfCategory class:
def subcat(parent_cat)
        self.find_all "cParent = #{parent_cat}"
end
That''s a rather strangely named model, by the way. I wouldn''t
recommend camel casing the database fields either.
Tomas
On Apr 8, 2005 10:40 PM, Dariusz S
<macdar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi,
> I''m totally new to rails. I''m trying to populate data
from mysql. I''ve
> got a problem when it comes to passing a parameter in a condition (for
> find_all). Definitions in a controller are as follow:
> 
> def list
>     @cf_categories = CfCategory.find_all  "cParent =
''0''"
> end
> 
> def subcat
>         @subcats = CfCategory.find_all cParent = @params["id"]
> end
> 
> and I don''t know how to declare the condition in the second
definition
> (cParent = @params["id"]). I''ve also checked that the id
is being
> passed from the list def to the subcat.
> 
> Can anybody help me?
> 
> Thanks.
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Rob Park
2005-Apr-08  22:58 UTC
Re: problem with passing a parameter to a condition (for find_all) - newbie question
On Apr 8, 2005 4:37 PM, Tomas Jogin <tomasj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I think this is what you''re looking for: > > def subcat > @subcats = CfCategory.find_all "cParent = #{@params["id"]}" > endPlease don''t ever suggest this again. http://manuals.rubyonrails.com/read/chapter/43 -- Urban Artography http://artography.ath.cx