I have this problem, but don't know why.
I create models with generator:
bin/rails generate model Book name:string author:string description:text cover:string
bin/rails generate model Episode name:string description:text art:string
ant other...
book.rb
class Book < ApplicationRecord
has_many :episodes
end
episode.rb
class Episode < ApplicationRecord
belongs_to :book
has_many :scenes
end
When in console i try to: book.episodes << episode i had an error: ActiveModel::MissingAttributeError: can't write unknown attribute "book_id"
My schema.rb
ActiveRecord::Schema.define(version: 20170320111956) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "answers", force: :cascade do |t|
t.text "text"
t.string "next_scene"
t.string "next_episode"
t.string "voiceover"
t.integer "end"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "books", force: :cascade do |t|
t.string "name"
t.string "author"
t.text "description"
t.string "cover"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "episodes", force: :cascade do |t|
t.string "name"
t.text "description"
t.string "art"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "scenes", force: :cascade do |t|
t.string "name"
t.text "text"
t.integer "choise"
t.string "next_scene"
t.string "next_episode"
t.string "art"
t.string "music"
t.string "sound_fx"
t.string "voiceover"
t.integer "end"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
In schema i haven't id_book, but why? I also do db:migrate and have error again.
By default, Rails uses the convention of naming the primary as the autogenerated column id. If you wish to specify a different primary key, such as book_id, you could do the following:
class Book < ApplicationRecord
self.primary_key = "book_id"
end
Related
I am still relatively new to Active Record and have a (hopefully simple) question.
I have four tables Recipes(Name of recipe), Food(lettuce, Pepper), Units(oz, tbsp), and Ingredients(Id's of the other tables and the numeric quantity).
What I'd like to do is something like this Recipes.Ingredients and get "Peppered Lettuce, 5 tbsp pepper, 10 oz Lettuce".
How would I accomplishes that with the following schema. And if not possible with this schema what should I build in its place.
The Schema looks like this:
create_table "food", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "ingredients", force: :cascade do |t|
t.bigint "recipes_id"
t.bigint "units_id"
t.bigint "food_id"
t.decimal "quantity"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["food_id"], name: "index_ingredients_on_food_id"
t.index ["recipes_id"], name: "index_ingredients_on_recipes_id"
t.index ["units_id"], name: "index_ingredients_on_units_id"
end
create_table "recipes", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
end
create_table "units", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
so what I understand from your question is you want to get results like:
Peppered Lettuce, 5 tbsp pepper, 10 oz Lettuce in which Peppered Lettuce is Recipe name, and Pepper and Lettuce are Food items with numeric quantities of ingredients.
You don't need 4 tables to obtain this result. You only need 3. Food, Recipe and an in-between table for their many-to-many association.
Recipe can have multiple Food items in it and 1 Food item can be a part of multiple Recipe objects. So Food and Recipe model will have a many-to-many association. And for this kind of association, you need another table. You can name it Foods_Recipes or simply Ingredients.
Your Ingredient model will then have a food_id,recipe_id,numeric_quantity and unit_type
The Schema would look like this:
create_table "foods", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "ingredients", force: :cascade do |t|
t.bigint "recipe_id"
t.bigint "food_id"
t.decimal "quantity"
t.string "unit_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["food_id"], name: "index_ingredients_on_food_id"
t.index ["recipe_id"], name: "index_ingredients_on_recipe_id"
end
create_table "recipes", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
end
Your models would look like this:
Food model:
class Recipe < ApplicationRecord
has_many :ingredients
has_many :foods,through: :ingredients
end
Ingredient model:
class Ingredient < ApplicationRecord
belongs_to :recipe
belongs_to :food
end
Food model:
class Food < ApplicationRecord
has_many :ingredients
has_many :recipes,through: :ingredients
end
I am working on a simple support ticket system , however I'm getting a weird error. Here is the error :
ActiveModel::MissingAttributeError: can't write unknown attribute division_id
and here is the code :
user.rb:
class User < ApplicationRecord
belongs_to :division
has_many :teckets
has_many :teckets , through: :user_tecket
has_secure_password
end
division.rb:
class Division < ApplicationRecord
has_many :users
has_many :users, through: :user_division
has_many :teckets
has_many :teckets, through: :teckets_divisions
end
tecket.rb :
class Tecket < ApplicationRecord
belongs_to :division
belongs_to :user
has_one :status
has_one :status, through: :tecket_statuses
end
teckets_division.rb :
class TecketsDivision < ApplicationRecord
belongs_to :tecket
belongs_to :division
end
schema.rb:
ActiveRecord::Schema.define(version: 20171001063612) do
create_table "divisions", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "replies", force: :cascade do |t|
t.string "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "statuses", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "tecket_replies", force: :cascade do |t|
t.integer "tecket_id"
t.integer "reply_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "tecket_statuses", force: :cascade do |t|
t.integer "tecket_id"
t.integer "status_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "teckets", force: :cascade do |t|
t.string "title"
t.string "content"
t.string "platform"
t.string "version"
t.string "client_anme"
t.string "client_phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "teckets_divisions", force: :cascade do |t|
t.integer "tecket_id"
t.integer "division_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "user_divisions", force: :cascade do |t|
t.integer "user_id"
t.integer "division_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "user_teckets", force: :cascade do |t|
t.integer "user_id"
t.integer "tecket_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "passwrod"
t.string "password_digest"
t.integer "division_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
I get the error when I try to create a user from the console
Edit : I have added the schema.rb file as requested
either remove the division_id from the user table and has_many :users from divisions model
or (and the simpler approach),
remove user_divisions join table and this line from division model:
has_many :users, through: :user_division
also - You have a lot of join tables it seems, but I don't see the need for their use. You could direct relate them in most of the cases you have shown.
I am relatively new to RoR.
This works nicely:
<td><%= collection_select :competitions_members, :member_id, Member.all, :id, :first_name %></td>
This one picks no value (actually all such calls to tables with translations):
<td><%= collection_select :competitions_members, :tull_id, Tull.all, :id, :name %></td>
seeded data in competitions_members table
Member can be involved in many competition. Basically I have N:M relationship between members and competitions via competitions_members table.
Tull is a dictionary. Value to be set during the process of assigning members to a competition.
Data model classes:
class Member < ApplicationRecord
has_and_belongs_to_many :competitions
end
class Competition < ApplicationRecord
has_and_belongs_to_many :members
end
class CompetitionsMember < ApplicationRecord
end
Tull table has also translations in separate table.
class Tull < ApplicationRecord
translates :name
has_many :competitions_members
# separate different localizations of the record
def cache_key
super + '-' + Globalize.locale.to_s
end
end
Relevant schema.db excerpt
create_table "members", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "competitions", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "competitions_members", force: :cascade do |t|
t.integer "member_id"
t.integer "competition_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "tull_id"
t.index ["tull_id"], name: "index_competitions_members_on_tull_id"
end
create_table "tull_translations", force: :cascade do |t|
t.integer "tull_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.index ["locale"], name: "index_tull_translations_on_locale"
t.index ["tull_id"], name: "index_tull_translations_on_tull_id"
end
create_table "tulls", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Any help apreciated. I just realized this might be connected with translated tables somehow.
class Tull < ApplicationRecord
has_many :translations
translates :name, :fallbacks_for_empty_translations => true
attr_accessible :translations_attributes
end
Try to execute below code in rails console:
Tull.first.translations - If this gives you translation records that means the associations are correct.
Now check at view side, how would you generate attributes for multilingual stuffs. I would suggest to use globalize_accessors.
Please send me the codebase.
I am having trouble migrating my database to Heroku. I have checked the other issues that address this to no avail. I can really use a second pair of eyes on my code to help me figure this out.
This is the error I get:
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "props" does not exist
: ALTER TABLE "comments" ADD CONSTRAINT "fk_rails_1d3f70cf04"
FOREIGN KEY ("prop_id")
REFERENCES "props" ("id")
It seems to get caught while migrating this file:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :prop, index: true, foreign_key: true
t.timestamps null: false
end
end
end
This is the migration file where I create the table props:
class CreateProps < ActiveRecord::Migration
def change
create_table :props do |t|
t.string :title
t.text :text
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
My schema is here:
ActiveRecord::Schema.define(version: 20160528205746) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "answers", force: :cascade do |t|
t.string "choice"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "created_by"
t.integer "user_id"
t.integer "prop_id"
end
create_table "comments", force: :cascade do |t|
t.string "commenter"
t.text "body"
t.integer "prop_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["prop_id"], name: "index_comments_on_prop_id", using: :btree
create_table "props", force: :cascade do |t|
t.string "title"
t.text "text"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "choice"
t.string "answer"
t.integer "answerId"
end
add_index "props", ["user_id"], name: "index_props_on_user_id", using: :btree
create_table "user_answers", force: :cascade do |t|
t.integer "user_id"
t.integer "answer_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: :cascade do |t|
t.string "username"
t.string "email"
t.integer "score", default: 0
t.integer "prop_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "created_by"
t.boolean "admin", default: false
t.integer "answers_id"
t.integer "answer_id"
end
add_index "users", ["answer_id"], name: "index_users_on_answer_id", using: :btree
add_index "users", ["prop_id"], name: "index_users_on_prop_id", using: :btree
create_table "wins", force: :cascade do |t|
t.string "correctAnswer"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "wins", ["user_id"], name: "index_wins_on_user_id", using: :btree
add_foreign_key "users", "answers"
end
The problem is you are creating a reference to a table that is not yet created. Remove the reference from that migration to props, then add the props table and then add a migration implementing the association. If you dont need the data currently in the db I would do a "rake db:drop" and edit the migration files (only if you arent collaborating with others!)
Update:
Do rails g migration add_ref_to_comments
Then edit the migration to have:
def change
add_reference :props, :comment, index: true
end
I'm rails begginer.
how to load database that has following condition.
I want show lecture list that has order by comment.created_at DESC
schema.rb
create_table "comments", force: :cascade do |t|
t.text "content"
t.integer "user_id"
t.integer "lecture_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "likedcount", default: 0
end
create_table "lectures", force: :cascade do |t|
t.string "subject"
t.string "professor"
t.string "major"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "uptachi", default: 0
t.integer "hatachi", default: 0
end
lecture.rb
has_many :comments
comment.rb
belongs_to: lectures
You need to write it as:
has_many :comments, -> { order("comments.created_at DESC") }
As per the OP's comment, it seems, what he want is a named scope. Like,
scope :order_by_comments, -> { joins(:comments).order("comments.created_at DESC") }