下面的代码在Rails 3中工作正常,但它会引发错误:
ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous column name:
id: SELECT "categories".id FROM "categories"
INNER JOIN "categorizations"
ON "categories"."id" = "categorizations"."category_id"
WHERE "categorizations"."reason_id" = 283 ORDER BY id
问题似乎是对reason.category_ids的调用
- reasons.each do |reason|
- cat_ids = reason.category_ids.map {|id| "cat_id_#{id}"}.join(" ")
%li{"data-reason-id" => reason.id, :class => cat_ids}
%a= reason.text
class Reason < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
更新: 在这里注释掉最后一行时,问题就解决了。这是一个错误吗?
class Category < ActiveRecord::Base
has_many :categorizations
has_many :reasons, :through => :categorizations
scope :active, where("active = ?", true)
default_scope :order => 'id'
答案 0 :(得分:4)
Here we go ...
[In Rails 3.1] Supplying options hash to with_scope, with_exclusive_scope and default_scope has also been deprecated:
default_scope :order => "id DESC"
Try:
default_scope order('categories.id')