Pregunta

I know the title is not accurate but I could not find another way to write an explicit question title.

In my MySQL database I have attachments which look like this:

#<Attachment id: 949, container_id: nil, container_type: nil, filename: "Quake_3.zip", disk_filename: "140515160454_Quake_3.zip", filesize: 491652766, content_type: "", digest: "84540939cc90c65bf7e101d0069da298", downloads: 0, author_id: 3, created_on: "2014-05-15 14:05:08", description: nil, disk_directory: "2014/05">

What I am trying to do is to get this entry but when I need it I don't know the id therefore I can't use this information. I would like to do my query with the filename and the container_id, I tried the following:`

att = Attachment.where("filename = ? AND container_id = 'nil'", fileName)

att = Attachment.where("filename = ? AND container_id = ?", fileName, nil)

and many things but nothing seems to work. It's probably a common question but I did not find how to perform this query.

Could you help me to get this entry with these two parameters? (description would work as well)

I'm working with Rails 3.2.17

Thanks

¿Fue útil?

Solución

Attachment.where("filename = ? AND container_id IS NULL", fileName)

or

Attachment.where(filename: fileName, container_id: nil)

Otros consejos

I guess the problem is that when checking for SQL nil/NULL values you need to use the IS comparator and not =.

Rails will generate the correct query if you do not use a string based where clause:

att = Attachment.where(filename: name, container_id: cid)

This should generate a valid query with nil values and normal values for name and cid.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top