Image
与 有one-to-many
关联Category
:
# Category model:
has_many :images
# Image model:
has_one :category
# Images migration file:
t.references :category, index: true, foreign_key: true
我正在尝试使用wice_grid gem实现网格视图。这有效,但一条线失败了。以下在索引视图中起作用:
g.column name: 'Category', attribute: 'category_id', auto_reload: true, html: {id: 'grid-cells'} do |image|
image.category_id
end
但这会显示类别的编号/ID,而我希望它显示其名称。如果我尝试image.category
而不是image.category_id
出现错误:
PG::UndefinedColumn: ERROR: column categories.image_id does not exist
LINE 1: SELECT "categories".* FROM "categories" WHERE "categories"."image_id...
如何让它显示类别名称而不是类别的 ID?
控制器方法:
def index
@images_grid = initialize_grid(Image,
# include: :category # Do I need this? Tried it with and without
per_page: 40)
end
更新:更正关联并在视图中使用下面的代码后,它可以工作。此特定列的过滤器功能除外。如果我输入要过滤的文本,它会重新加载表格,但不应用过滤器(仍然显示所有记录)。
g.column name: 'Category', attribute: 'category_id', auto_reload: true, html: {id: 'grid-cells'} do |image|
image.category.category unless image.category.nil?
end