简体   繁体   中英

wrong number of arguments (0 for 1) Rails error

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM