Rails协会(Rails Associations)
我似乎无法绕过Rails关联。 假设我有3张桌子。 一个用于用户,一个用于食谱,另一个用于配料。 以下是数据库的示例(在yaml中):
Users: id: 1 email: funnyusername password: pass1234 Recipes: id: 1591 user_id: 1 name: Pizza Dough Ingredients: id: 1 name: Flour id: 2 name: Water id: 3 name: Yeast
现在我该怎么做,以便用户可以有很多食谱,一个食谱可以有很多成分。 还能够找到每种配料所附的所有配方?
例如,funnyusername创建了配方“比萨面团”,其中配有面粉,水和酵母。 现在,当我搜索酵母时,我希望它能拿出一份包含酵母的食谱列表。
I can't seem to wrap my mind around Rails associations. Let's say I have 3 tables. One for Users, one for Recipes, and one for Ingredients. Here's an example of the database (in yaml):
Users: id: 1 email: funnyusername password: pass1234 Recipes: id: 1591 user_id: 1 name: Pizza Dough Ingredients: id: 1 name: Flour id: 2 name: Water id: 3 name: Yeast
Now how would I make it so that a User can have many Recipes, and a Recipe can have many Ingredients. And also be able to find all the Recipes each Ingredient is attached to?
For instance, funnyusername created the Recipe "Pizza Dough" which has the ingredients Flour, Water, and Yeast. Now when I search for Yeast, I would want it to come up with a list of Recipes containing Yeast.
原文:https://stackoverflow.com/questions/1414214
最满意答案
# Add file to our zip zf.write("zippy2.py")
您应该在该文件夹中有一个名为zippy2.py的文件。
由于您刚刚复制了代码,因此可能没有代码中提到的文件。 在同一文件夹中创建文件zippy2.py并检查。
# Add file to our zip zf.write("zippy2.py")
You should have a file named zippy2.py in the folder.
Since you just copied the code, you might not have the file that was mentioned in the code. create file zippy2.py in the same folder and check.
相关问答
更多-
创建图像的.Zip文件(Create .Zip file of Images)[2023-10-28]
你可以通过在MFMailComposeViewController上调用addAttachmentData:mimeType:fileName:来做到这一点 - 如果你已经有了zip文件。 如果您的问题包括创建zip文件,遗憾的是,在iOS中进行一些方法调用是不可能的。 您需要使用第三方库,例如objective-zip 。 You can do that by calling addAttachmentData:mimeType:fileName: on the MFMailComposeViewCont ... -
您正在查看的示例代码完全取决于Windows,使用C:\blah路径。 OS X上的等效路径如下所示: /Users/yourusername/Documents (在转换代码时,你似乎也以某种方式改变了一些东西。不确定那是什么。) The example code you're looking at is totally Windows specific, what with the C:\blah paths. An equivalent path on OS X would look somethi ...
-
你可以在将它添加到zip文件之前将chdir包含在整个目录路径中,然后使用os.remove删除文件: def create_zip(path,zipf): for root, dirs, files in os.walk(path): for file in files: if file.endswith(".json"): chdir(root) zipf.write(file) ...
-
有一个使用zipfile的例子: import os, zipfile from os.path import join def zipfolder(foldername, filename, includeEmptyDIr=True): empty_dirs = [] zip = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED) for root, dirs, files in os.walk(folde ...
-
Python:无法创建Zip文件(Python: Cannot Create Zip File)[2023-01-15]
# Add file to our zip zf.write("zippy2.py") 您应该在该文件夹中有一个名为zippy2.py的文件。 由于您刚刚复制了代码,因此可能没有代码中提到的文件。 在同一文件夹中创建文件zippy2.py并检查。 # Add file to our zip zf.write("zippy2.py") You should have a file named zippy2.py in the folder. Since you just copied the code, ... -
您使用ZipFile.write 第二个参数来设置存档中文件的正确名称: import os.path ... myzip.write(file_to_zip, os.path.basename(file_to_zip)) You shuld use second argument of ZipFile.write to set proper name of file in archive: import os.path ... myzip.write(file_to_zip, os.path.b ...
-
使用 df.to_csv('my_file.gz', compression='gzip') 来自文档: compression:string,可选表示要在输出文件中使用的压缩的字符串,允许的值为'gzip','bz2','xz',仅在第一个参数是文件名时使用 看起来很快就会添加 zip 。 Use df.to_csv('my_file.gz', compression='gzip') From the docs: compression : string, optional a string repr ...
-
您可以在addFile方法中指定文件名。 这并不明显,因为周围没有明显的例子,但您可以使用$localname (第二个参数)来定义和控制zip中的文件/目录结构。 如果您不希望文件包含在其绝对目录树中,请使用它。 这是一个使用basename的工作示例: $file1 = 'D:/xampp/htdocs/pdf/pdffiles/14816393105-annexc-form.pdf'; // ... $zip->addFile($file1, basename($file1)); basenam ...
-
创建Zip文件Fedora 17(Create Zip File Fedora 17)[2022-11-17]
zip myArchiveName *.{prj,shp,shx} 这会将列出您的扩展名的所有文件压缩到名为myArchiveName.zip的zip文件中 zip myArchiveName *.{prj,shp,shx} This will zip all files with your extensions listed into a zip file named myArchiveName.zip -
JSZip支持promises作为内容:您可以将每个HTTP调用包装到promises中,而不是显式等待。 第一个函数downloadUrlAsPromise将xhr调用包装成Promise。 第二个函数downloadFirebaseImage将getDownloadURL的promise与第一个函数的promise相链接。 结果是xhr内容的承诺。 一旦你有了,你可以直接给JSZip这样的承诺: zip.file(i+".png", downloadFirebaseImage(imagePath)); ...