开发者

wrong number of arguments (0 for 1) Rails error

开发者 https://www.devze.com 2023-02-12 19:53 出处:网络
I have an error for this method below def mymethod cat = Cat.find_by_cat_id_and_animal_id_and_name(catid, haustierid, cat_name)

I have an error for this method below

 def mymethod
    cat = Cat.find_by_cat_id_and_animal_id_and_name(catid, haustierid, cat_name)
    if cat.name.nil?
       if cat.name == "beauty"
           #doworkhere
        end
      end
 end

The thing is that, I have a cat table, with a column of name. Where inside the cat name, there are different rows containing the name ("beauty", "ugly", etc.). I tried to basically loop in the cat table, going into 开发者_如何学运维the rows and check if "beauty" is there, then do something. However, it returns that I am entering wrong arguments. I can not understand about the wrong arguments , is this the correct way of going in a loop inside the "cat name column" Thank you for any answer.


I think your find query should be:

cat = Cat.find_by_id_and_animal_id_and_name(catid, haustierid, cat_name)

and not

cat = Cat.find_by_cat_id_and_animal_id_and_name(catid, haustierid, cat_name)

Thanks....


There are several errors in your code but no one can explain the error message you receive. You probably need to post the code inside the if.

In the meanwhile, let me show a few issues

cat = Cat.find_by_cat_id_and_animal_id_and_name(catid, haustierid, cat_name)

You probably mean

cat = Cat.find_by_id_and_animal_id_and_name(catid, haustierid, cat_name)

Also, you might want to use scopes (or named scopes if you use Rails 2)

if cat.name.nil?
   if cat.name == "beauty"
       #doworkhere

I won't ever enter the second if. A name cannot be nil and a String at the same time. Also, assuming you wanted to say !nil? you can write it in a more concise way by doing

if !cat.name.nil? && cat.name == "beauty"
    #doworkhere

or simply

if cat.name == "beauty"
    #doworkhere
0

精彩评论

暂无评论...
验证码 换一张
取 消