Can't sort by rails index - ruby-on-rails

I used the rails globalize and I18n gem. But now I can't sort my model. Can you guys help?
I tried adding a new index, but I'm not entirely familiar with indexing.
Controller.rb
def index
#foods = Food.all.order(:name)
add_breadcrumb "index", foods_path
end
Schema
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

Yes, you will need a join. If you do not have a model for the translation, you could just use .joins for your finder. Like:
Food.joins('INNER JOIN food_translations ON foods.id=food_translations.food_id')
.order('food_translations.name').where('food_translations.locale=xxx')
ps: I wonder why you do not have a index on "food_id" AND "locale" which should be uniq. In your case you can have two or more translations for 1 food in the same language.

Related

Error- ActiveModel::UnknownAttributeError: unknown attribute 'name' for Review

When I run:
rails db:seed
in the command line, I get the following error:
ActiveModel::UnknownAttributeError: unknown attribute 'name' for Review..........................................................
I also get the same error when I run:
rails db:seed
seeds.rb
movie = Movie.find_by(title: 'Iron Man')
movie.reviews.create!(name: "Roger Ebert", stars: 3, comment: "I laughed, I cried, I spilled my popcorn!")
movie.reviews.create!(name: "Gene Siskel", stars: 5, comment: "I'm a better reviewer than he is.")
movie.reviews.create!(name: "Peter Travers", stars: 4, comment: "It's been years since a movie superhero was this fierce and this funny.")
movie = Movie.find_by(title: 'Superman')
movie.reviews.create!(name: "Elvis Mitchell", stars: 5, comment: "It's a bird, it's a plane, it's a blockbuster!")
Genre.create!(name: "Action")
Genre.create!(name: "Comedy")
Genre.create!(name: "Drama")
Genre.create!(name: "Romance")
Genre.create!(name: "Thriller")
Genre.create!(name: "Fantasy")
Genre.create!(name: "Documentary")
Genre.create!(name: "Adventure")
Genre.create!(name: "Animation")
Genre.create!(name: "Sci-Fi")
Here is my schema filee [sic]:
ActiveRecord::Schema.define(version: 20181218222845) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "characterizations", force: :cascade do |t|
t.bigint "movie_id"
t.bigint "genre_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["genre_id"], name: "index_characterizations_on_genre_id"
t.index ["movie_id"], name: "index_characterizations_on_movie_id"
end
create_table "favorites", force: :cascade do |t|
t.bigint "movie_id"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["movie_id"], name: "index_favorites_on_movie_id"
t.index ["user_id"], name: "index_favorites_on_user_id"
end
create_table "genres", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "movies", force: :cascade do |t|
t.string "title"
t.string "rating"
t.decimal "total_gross"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "description"
t.date "released_on"
t.string "cast"
t.string "director"
t.string "duration"
t.string "image_file_name", default: ""
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "main_image"
t.string "slug"
end
create_table "reviews", force: :cascade do |t|
t.integer "stars"
t.text "comment"
t.bigint "movie_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.index ["movie_id"], name: "index_reviews_on_movie_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.boolean "admin", default: false
end
add_foreign_key "characterizations", "genres"
add_foreign_key "characterizations", "movies"
add_foreign_key "favorites", "movies"
add_foreign_key "favorites", "users"
add_foreign_key "reviews", "movies"
end
Your reviews table does not have a name column, which is what Rails is telling you.
I don’t know how your app works, but I do see a user_id column on the reviews table, and the users table has a name column, so I imagine you need to create a user using that name attribute, and then pass the user into the review that you’re creating.
Without seeing your Review and User model, I can’t speak for sure.
At a minimum, you'll need to create a migration & run it to add the name field to your reviews table:
rails generate migration AddNameToReviews name:string
rails db:migrate
This should fix your reported error.

Rails: how to create a migration that add 'using: :btree' to foreign key

I have two models
father has_many sons
son belongs_to father
Normally, this piece of code
def change
add_reference :sons, :father, index: true
end
it will generate
add_index "sons", ["father_id"], name: "index_sons_on_father_id"
Now, I want it generate
add_index "sons", ["father_id"], name: "index_sons_on_father_id", using: :btree
How to write the migration?
you just check this
you can do look like this.
ActiveRecord::Schema.define(version: 20150321171548) do
create_table "authors", force: :cascade do |t|
t.string "name"
t.string "hashtags"
t.string "avatar"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "articles", force: :cascade do |t|
t.integer "author_id", limit: 4
t.string "title", limit: 255
t.string "bannerurl", limit: 255
t.string "thumbnailurl", limit: 255
t.text "content", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "articles", ["author_id"], name: "index_articles_on_author_id", using: :btree
end

rails eager loading for one-to-many association

I have
class Article < ActiveRecord::Base
has_one :template
end
and
class Template < ActiveRecord::Base
has_many :articles
end
where Template model has got a room property. Now i'd like to build a list of all articles, where the articles template has a certain room value (say "bath").
I thought this is done by eager loading (resp: includes), but if i try
Article.includes(:template)
I get the error
SELECT "templates".* FROM "templates" WHERE "templates"."article_id" IN ('51', '52', '53', '54')
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column templates.article_id does not exist
LINE 1: SELECT "templates".* FROM "templates" WHERE "templates"."art...
What am I doing wrong?
EDIT
here's my schema.rb as asked
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160913122551) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "details"
t.integer "value_eur"
t.integer "deposit_eur"
t.integer "location_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "picture"
t.float "rate_eur"
t.string "rate_interval"
t.integer "template_id"
t.integer "quality"
end
add_index "articles", ["location_id"], name: "index_articles_on_location_id", using: :btree
add_index "articles", ["template_id"], name: "index_articles_on_template_id", using: :btree
add_index "articles", ["user_id"], name: "index_articles_on_user_id", using: :btree
create_table "locations", force: :cascade do |t|
t.string "street_and_no"
t.string "postcode"
t.string "city"
t.string "country"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "latitude"
t.float "longitude"
end
add_index "locations", ["user_id"], name: "index_locations_on_user_id", using: :btree
create_table "templates", force: :cascade do |t|
t.string "title"
t.text "details_hint"
t.float "rate_eur"
t.string "rate_interval"
t.string "picture"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "room"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "role"
t.string "nickname"
t.string "firstname"
t.string "lastname"
t.string "phoneno"
t.boolean "showemail"
t.boolean "showphone"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["nickname"], name: "index_users_on_nickname", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_foreign_key "articles", "locations"
add_foreign_key "articles", "templates"
add_foreign_key "articles", "users"
add_foreign_key "locations", "users"
end
Your templates table does not have an article_id column according to the schema.rb you posted so you will need to create that reference.
Change
has_one :template
in the articles model to
belongs_to :template

Creating A Nested Category List

Im creating an online retail store. Im trying to create a Category List. I have been able to make a category list but i need subcategories in a nested tree like structure.
Such as the following:
Mobile Phones
Apple
HTC
Samsung
Laptops
Sony
Apple
I have tried for 8 hours now and just keep getting stuck.
Im really stuck.
I tried Ancestry Gem.
How would you go about doing this in detail, even step by step would be great?
There are some tutorials but none that i can find that are directly show what im trying to do.
My Database schema.rb as requested.
ActiveRecord::Schema.define(version: 20150721095122) do
create_table "categories", force: :cascade do |t|
t.string "name"
t.string "ancestry"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "categories", ["ancestry"], name: "index_categories_on_ancestry"
create_table "items", force: :cascade do |t|
t.string "title"
t.decimal "price"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
add_index "items", ["user_id", "created_at"], name: "index_items_on_user_id_and_created_at"
add_index "items", ["user_id"], name: "index_items_on_user_id"
create_table "users", force: :cascade do |t|
t.string "username"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin", default: false
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.string ">"
t.datetime "reset_sent_at"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.text "description"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
You can also try using awesome_nested_set gem that supports nesting, and it has nice documentation, and a list of all methods

Rails Confused with refrential integrity

The more i read about foreign keys in rails i am getting more and more confused. In a post i read that its sufficient to add belongs_to and has_many/has_one in respective model file to getting things done. But again in another post I read that the index should be added to reference another table. Suppose There is writers table and book table in dbms while creating Books table we have to add
FOREIGN KEY (writers_Id) REFERENCES Writers(Id)
but in rails we in writer model we add has_many :book an in book model we add belongs_to :writer is both are equivalent ?
If both are equivalent then why we add index such as
add_index :books, :writer_id
I have project on which I am working on it has users has one personal information, academic information, application and rank. Also there is subject_streams which have streams and streams have cutoffs. Finally there is category which is independent. I dont know if i modeled data correctly but in the schema.rb am I doing correctly what i have said ?
ActiveRecord::Schema.define(version: 20140713133617) do
create_table "academics", force: true do |t|
t.integer "user_id"
t.integer "tenth_roll", default: 0
t.integer "tenth_year_pass", default: 2000
t.decimal "tenth_marks_percent", precision: 10, scale: 2, default: 40.0
t.string "tenth_board"
t.integer "hs_roll", default: 0
t.integer "hs_year_pass", default: 2002
t.decimal "hs_marks_percent", precision: 10, scale: 2, default: 40.0
t.string "hs_board"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "calculated_marks"
t.string "sub1"
t.integer "sub1_marks"
t.string "sub2"
t.integer "sub2_marks"
t.string "sub3"
t.integer "sub3_marks"
t.string "sub4"
t.integer "sub4_marks"
t.string "sub5"
t.integer "sub5_marks"
t.string "sub6"
t.integer "sub6_marks"
t.string "sub7"
t.integer "sub7_marks"
t.string "sub8"
t.integer "sub8_marks"
t.string "sub9"
t.integer "sub9_marks"
t.string "sub10"
t.integer "sub10_marks"
t.integer "subject_streams_id"
end
add_index "academics", ["user_id"], name: "index_academics_on_user_id", unique: true, using: :btree
create_table "applications", force: true do |t|
t.integer "user_id"
t.integer "stream_id"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "verified", default: false
end
add_index "applications", ["user_id"], name: "index_applications_on_user_id", unique: true, using: :btree
create_table "categories", force: true do |t|
t.string "category"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "cutoffs", force: true do |t|
t.integer "stream_id"
t.integer "category_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "cutoff_marks"
end
add_index "cutoffs", ["stream_id"], name: "index_cutoffs_on_stream_id", using: :btree
create_table "personals", force: true do |t|
t.integer "user_id"
t.date "date_of_birth"
t.string "gender"
t.string "blood_group"
t.string "fathers_name"
t.string "mothers_name"
t.text "address_present"
t.datetime "created_at"
t.datetime "updated_at"
t.string "first_name"
t.string "middle_name"
t.string "last_name"
t.integer "category_id"
t.string "image"
t.string "avatar"
end
add_index "personals", ["user_id"], name: "index_personals_on_user_id", unique: true, using: :btree
create_table "ranks", force: true do |t|
t.integer "user_id"
t.integer "rank"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "ranks", ["user_id"], name: "index_ranks_on_user_id", unique: true, using: :btree
create_table "registers", force: true do |t|
t.boolean "active"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "simple_captcha_data", force: true do |t|
t.string "key", limit: 40
t.string "value", limit: 6
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "simple_captcha_data", ["key"], name: "idx_key", using: :btree
create_table "streams", force: true do |t|
t.string "stream"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "seats"
t.integer "subject_stream_id"
end
add_index "streams", ["subject_stream_id"], name: "index_streams_on_subject_stream_id", using: :btree
create_table "subject_streams", force: true do |t|
t.string "subject_stream"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "level", default: 1
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
In Ruby on Rails, the code has_many :books and belongs_to :writer is like defining relationships in the code level. It doesnt achieve anything in the database level. For example, if we write belongs_to :writer in the Book model, it just means that if there is a column writer_id in the books table, we can code something like this:
b = Book.first
b_writer = b.writer
#### equivalent to
# b_writer = Writer.find(b.writer_id)
# b_writer = Writer.where(:id => b.writer_id).first
# or b_writer = Writer.find_by_sql("SELECT writers.* from writers where writers.id = #{b.writer_id}")
It assumes that the foreign key is writer_id in books table by convention. If we need to change the foreign key for the queries generated by association, we need to specify it separately:
belongs_to :writer, :foreign_key => 'writerID'
So, in general declaring associations in models, gives us some utility methods to query those associations from database, instead of creating the queries manually. So, all the database related changes needs to be done in the migrations, like adding the column writer_id, adding index for column writer_id, setting writer_id with forieign key constraint etc. Rails does not support foreign_key constraint in migrations by default, as different databases handle foreign_key differently. For PostGre and MySQL database, you can use foreigner gem for adding foreign_key constraint.
Please read more about Rails associations

Resources