【发布时间】:2016-11-22 00:49:31
【问题描述】:
我使用了 rails globalize 和 I18n gem。但现在我无法对我的模型进行排序。你们能帮忙吗?
我尝试添加新索引,但我对索引并不完全熟悉。
控制器.rb
def index
@foods = Food.all.order(:name)
add_breadcrumb "index", foods_path
end
架构
create_table "food_translations", force: :cascade do |t|
t.integer "food_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "bio"
end
add_index "food_translations", ["food_id"], name: "index_food_translations_on_food_id", using: :btree
add_index "food_translations", ["locale"], name: "index_food_translations_on_locale", using: :btree
add_index "food_translations", ["name"], name: "index_food_translations_on_name", using: :btree
create_table "foods", force: :cascade do |t|
t.string "address"
t.string "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "yelp"
t.string "youtube"
end
【问题讨论】:
-
name是FoodTranslation的属性,而不是Food的属性。您没有在那里显示任何关系,但您需要以某种方式包含food_translations表以便能够对food_translations.name进行排序。 -
我明白了。我尝试加入(FoodTranslations.name),但语法肯定是错误的。
-
重点是,您应该在问题中包含模型类以及相关关联(例如
belongs_to)。 -
这是否意味着我必须创建一个新的model.rb? Food Translations 是由 gem 制作的。
-
某处必须有一些代码将食物翻译表映射到 ruby 对象。您还可以包含您使用的 gem 的名称,以防有人(不是我)熟悉它以及它的确切作用。但我可以告诉你,你想做一个联接,而在 ActiveRecord 中进行联接最自然的方法是使用关联。
标签: ruby-on-rails ruby indexing