Ruby on rails 多对多关联的列名称不明确
我正在尝试为多对多关系创建复选框 一个Ruby on rails 多对多关联的列名称不明确,ruby-on-rails,ruby-on-rails-4,associations,rails-activerecord,Ruby On Rails,Ruby On Rails 4,Associations,Rails Activerecord,我正在尝试为多对多关系创建复选框 一个项目可以有许多图形。一个图可以属于许多项目 我正在跟踪的答案,并使用collection\u复选框() 强制排序UserGraph.all使用从user\u graph表中创建的 <%= collection_check_boxes(:project, :user_graph_ids, UserGraph.all.order("user_graphs.created_at ASC"), :id, :title) %> 仍然无法使用最上面的更改。
项目
可以有许多图形
。一个图
可以属于许多项目
我正在跟踪的答案,并使用collection\u复选框()
强制排序
UserGraph.all
使用从user\u graph
表中创建的
<%= collection_check_boxes(:project, :user_graph_ids, UserGraph.all.order("user_graphs.created_at ASC"), :id, :title) %>
仍然无法使用最上面的更改。我以为你忘记了ASC之后的关闭偏执“
:
-错误是相同的。有趣的是,我们指定UserGraph的顺序不会影响SQL查询。也许查询是由Project+UserGraph之间的关联自动创建的?奇怪。你能在UserGraph
模型中发布关联吗?是的,现在编辑这个问题,顺便说一句,用你的新代码它给出了这个错误错误数量的参数(3代表5..7)
看起来偏执仍然在错误的地方:P
SQLite3::SQLException: ambiguous column name: created_at:
SELECT "user_graphs".id
FROM "user_graphs"
INNER JOIN "project_user_graphs"
ON "user_graphs"."id" = "project_user_graphs"."user_graph_id"
WHERE "project_user_graphs"."project_id" = ?
ORDER BY created_at DESC
<%= collection_check_boxes(:project, :user_graph_ids, UserGraph.all.order("user_graphs.created_at ASC"), :id, :title) %>
class UserGraph < ActiveRecord::Base
def self.ordered(direction="asc")
order("user_graphs.created_at #{direction}")
end
end
<%= collection_check_boxes(:project, :user_graph_ids, UserGraph.all.ordered, :id, :title) %>