Application_controller define method to only show future dates - ruby-on-rails

In my Ruby on Rails application I have a cinema system and am trying to show only show times for films that are either in the future or today (so not in the past).
I am trying to do this in my _form.html.erb in a drop down menu:
<%= f.grouped_collection_select :showing_id, live_films.order(:title), :showings, :title, :id, :showing_times %>
Where live_films is the method in application_controller.rb:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :active_menu, :live_films
def live_films
Film.includes(:showings).where('showings.show_date > ?', Date.current.beginning_of_day)
end
end
But I get this error:
ActiveRecord::StatementInvalid in Bookings#new
SQLite3::SQLException: no such column: showings.show_date: SELECT "films".* FROM "films" WHERE (showings.show_date > '2015-02-20 00:00:00.000000') ORDER BY "films"."title" ASC
My db/schema:
ActiveRecord::Schema.define(version: 20150219091141) do
create_table "bookings", force: :cascade do |t|
t.integer "user_id"
t.integer "showing_id"
t.integer "seat_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "categories", force: :cascade do |t|
t.string "genre"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "certificates", force: :cascade do |t|
t.string "age_rating"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "certificate_img_url"
end
create_table "films", force: :cascade do |t|
t.string "title"
t.string "synopsis"
t.string "director"
t.string "cast1"
t.string "cast2"
t.string "cast3"
t.date "release_date"
t.string "warnings"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_url"
t.string "certificate_id"
t.integer "category_id"
t.integer "hours"
t.integer "minutes"
t.string "video_url"
end
create_table "screens", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "seats", force: :cascade do |t|
t.string "row_letter"
t.integer "row_number"
t.integer "screen_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "showings", force: :cascade do |t|
t.date "show_date"
t.time "show_time"
t.integer "film_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "screen_id"
end
add_index "showings", ["film_id"], name: "index_showings_on_film_id"
create_table "users", force: :cascade do |t|
t.string "password_digest"
t.string "role"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.string "house_no"
t.string "street"
t.string "town"
t.string "postcode"
t.string "email"
end
The column name show_date is correct as is the table name showings but for some reason it isn't working.
Can someone please help?

Here is the solution:
Film.joins(:showings).where('showings.show_date > ?', Date.current.beginning_of_day).preload(:showings)

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 not getting value from associations

I have 2 tables named : order_items and orders in schema. Following is sample of my schema
create_table "orders", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "token"
t.decimal "sub_total"
t.string "mobile"
t.string "address"
and
create_table "order_items", force: :cascade do |t|
t.integer "order_id", null: false
t.integer "item_id", null: false
t.integer "quantity", null: false
t.decimal "price", precision: 15, scale: 2, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
For Models,
order.rb
has_many :order_items
order_item.rb
belongs_to :order
belongs_to :item
I thought I would have been able to get Order.orderitem.quantity with this association, but not able to get it. I get error as undefined method 'orderitems' for nil:NilClass
try Order.first.order_items
this will return all the order items for first order
then you can loop through them
Order.first.order_items.each do |item|
item.quantity
....
end
or take the item out with index
items = Order.first.order_items
items[0].quantity

How to correctly maintain database schema in rails?

I have schema like this:
create_table "grades", force: :cascade do |t|
t.integer "cls"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "post_grades", force: :cascade do |t|
t.integer "post_id"
t.integer "grade_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "posts", force: :cascade do |t|
t.integer "user_id", null: false
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "user_grades", force: :cascade do |t|
t.integer "user_id"
t.integer "grade_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
In this schema grades, posts and users all are related to each other.
So my first question: Is it correct way of doing this?
Suppose one of user added grade 4 (cls = 4) in his column and another user added same grade. Now I have same value of cls in grade table for two different grade ids.So is there any data redundancy in this schema?
As I got your task, you can go with such solution:
grades -> no references (as it is right now)
user_grades (conventional way to name this table would be grades_users... alphabetical order and both in plural) -> user_id, grade_id (as it is right now)
You don't need post_grades. It's redundant
posts -> grade_id, user_id (if you need author)
Your schema.rb will look smth like:
create_table "users", force: :cascade do |t|
...
end
create_table "grades_users", force: :cascade do |t|
t.integer "user_id"
t.integer "grade_id"
...
end
create_table "grades", force: :cascade do |t|
...
end
create_table "posts", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "grade_id", null: false
...
end

Can't sort by rails index

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.

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

Resources