Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I know there are quite a few questions on this topic here on SO but none of them seem to be the solution for my case. So, I was hoping someone might be able to help.
When I run a migration I get the following error:
CreateEvents: migrating
-- create_table(:events)
-> 0.0558s
-- add_index(:events)
rake aborted!
An error has occurred, this and all later migrations canceled:
wrong number of arguments (1 for 2)
I don't know exactly what I'm looking for and where. I wanted to change my Users table to a Registrations table.
Registrations Controller:
class RegistrationsController < ApplicationController
end
Registration Model:
class Registration < ActiveRecord::Base
attr_accessible :email, :password_digest
end
Registration Migration:
class CreateRegistrations < ActiveRecord::Migration
def change
create_table :registrations do |t|
t.string :email
t.string :password_digest
t.timestamps
end
end
end
Schema.rb:
ActiveRecord::Schema.define(:version => 20130825195829) do
create_table "activities", :force => true do |t|
t.integer "trackable_id"
t.string "trackable_type"
t.integer "owner_id"
t.string "owner_type"
t.string "key"
t.text "parameters"
t.integer "recipient_id"
t.string "recipient_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "activities", ["owner_id", "owner_type"], :name => "index_activities_on_owner_id_and_owner_type"
add_index "activities", ["recipient_id", "recipient_type"], :name => "index_activities_on_recipient_id_and_recipient_type"
add_index "activities", ["trackable_id", "trackable_type"], :name => "index_activities_on_trackable_id_and_trackable_type"
create_table "comments", :force => true do |t|
t.string "commenter"
t.text "body"
t.integer "event_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "comments", ["event_id"], :name => "index_comments_on_event_id"
create_table "events", :force => true do |t|
t.string "title"
t.text "text"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
add_index needs 2 arguments example: add_index :events, :title
Related
I've been having trouble to do a challenge with Active Records, I read the documentation, and seen other examples with belongs_to that I remade and worked, I have no clue anymore about what I'm doing wrong here when I try to call recipe.recipe_type.name I get the error Rails NoMethodError: undefined method `name' for "#":String
schema.rb
create_table "recipe_types", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "recipe_type_id"
t.index ["recipe_type_id"], name: "index_recipe_types_on_recipe_type_id"
end
create_table "recipes", force: :cascade do |t|
t.string "title"
t.string "cuisine"
t.string "difficulty"
t.integer "cook_time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "ingredients"
t.text "cook_method"
end
end
migrations
def change
create_table :recipes do |t|
t.string :title
t.string :cuisine
t.string :difficulty
t.integer :cook_time
t.timestamps
end
end
end
class AddFieldsToRecipe < ActiveRecord::Migration[5.2]
def change
add_column :recipes, :ingredients, :text
add_column :recipes, :cook_method, :text
end
end
class CreateRecipeTypes < ActiveRecord::Migration[5.2]
def change
create_table :recipe_types do |t|
t.string :name
t.timestamps
end
end
end
class AddRecipeRefToRecipeType < ActiveRecord::Migration[5.2]
def change
add_reference :recipe_types, :recipe_type, foreign_key: true
end
end
You seemed to have added the recipe_type reference to the wrong table. Your last migration should probably have been
add_reference :recipes, :recipe_type, foreign_key: true
because as it is, you have added the reference_type reference to ReferenceType.
So the final schema was:
ActiveRecord::Schema.define(version: 2020_03_26_013134) do
create_table "recipe_types", force: :cascade do |t|
t.string "name"
t.integer "recipe_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["recipe_id"], name: "index_recipe_types_on_recipe_id"
end
create_table "recipes", force: :cascade do |t|
t.string "title"
t.string "cuisine"
t.string "difficulty"
t.integer "cook_time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "ingredients"
t.text "cook_method"
t.integer "recipe_type_id"
t.index ["recipe_type_id"], name: "index_recipes_on_recipe_type_id"
end
end
and with
models/recipe_type
class RecipeType < ApplicationRecord
has_many :recipes
end
models/recipe
class Recipe < ApplicationRecord
belongs_to :recipe_type
end
You can start a simple has_many belongs_to active record association, I hope this helps as many people as those who helped me, in the beginning of this journey, strongly recommend to study db:migrate, db:rollback, db:create and db:drop for those who encounter some trouble.
This question already has answers here:
A migration to add unique constraint to a combination of columns
(6 answers)
Closed 7 years ago.
Based off http://guides.rubyonrails.org/v3.2.21/migrations.html and
given the following migration:
class CreateVacations < ActiveRecord::Migration
def change
create_table :vacations do |t|
t.string :name
t.string :slug, :uniqueness => true
t.datetime :starts_at
t.datetime :ends_at
t.timestamps
end
end
end
I ran rake db:migrate and see this schema:
ActiveRecord::Schema.define(:version => 20150825170615) do
create_table "vacations", :force => true do |t|
t.string "name"
t.string "slug"
t.datetime "starts_at"
t.datetime "ends_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
I do not ssee any uniqueness requirement for 'slug'. If I did it wrong, how can I fix it at this point?
If it was right, why doesn't the schema say it's unique? Thank you
You need to create an index in order to enforce uniqueness at the database level:
add_index :vacations, :slug, unique: true
I get this error:
rake aborted!
undefined method `meta_keywords' for Page::Translation:0x0000000548d5f0
when running rake db:setup on a refinery cms 1.0.0 rails 3.0.7.
and can't quite debug it
Edit
part of schema:
create_table "page_part_translations", :force => true do |t|
t.integer "page_part_id"
t.string "locale"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "page_part_translations", ["page_part_id"], :name => "index_page_part_translations_on_page_part_id"
create_table "page_parts", :force => true do |t|
t.integer "page_id"
t.string "title"
t.text "body"
t.integer "position"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "page_parts", ["id"], :name => "index_page_parts_on_id"
add_index "page_parts", ["page_id"], :name => "index_page_parts_on_page_id"
create_table "page_translations", :force => true do |t|
t.integer "page_id"
t.string "locale"
t.string "title"
t.string "custom_title"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "page_translations", ["page_id"], :name => "index_page_translations_on_page_id"
create_table "pages", :force => true do |t|
t.integer "parent_id"
t.integer "position"
t.string "path"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "show_in_menu", :default => true
t.string "link_url"
t.string "menu_match"
t.boolean "deletable", :default => true
t.string "custom_title_type", :default => "none"
t.boolean "draft", :default => false
t.boolean "skip_to_first_child", :default => false
t.integer "lft"
t.integer "rgt"
t.integer "depth"
end
add_index "pages", ["depth"], :name => "index_pages_on_depth"
add_index "pages", ["id"], :name => "index_pages_on_id"
add_index "pages", ["lft"], :name => "index_pages_on_lft"
add_index "pages", ["parent_id"], :name => "index_pages_on_parent_id"
add_index "pages", ["rgt"], :name => "index_pages_on_rgt"
pages.rb: https://gist.github.com/grzegorzhauska/dcca44f3e2091c21400a
pages_for_inqueries.rb: https://gist.github.com/grzegorzhauska/997c355c2ce09d1da624
The reason you get this error is because meta_keywords used to exist, but now it doesn't. Make sure you are using an up to date version of the seo_meta library.
I have two models...
create_table "registrations", :force => true do |t|
t.integer "orientation_id"
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "student_id"
...
end
create_table "orientations", :force => true do |t|
t.date "class_date"
t.text "class_time"
t.integer "seats"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
I want to create a validation in my registration model that says the student_id must be unique in each Orientation.
If i understood your question correctly,you want the scope option of the validates_uniqueness_of.If so,this should work
In your Registration model,
Class Registration < ActiveRecord::Base
......
validates_uniqueness_of :student_id, scope: :orientation_id
end
And also,you should be generating a migration to add this
add_index :registration, [ :student_id, :orientation_id ], :unique => true
More Info here
Setting up the database, I am just curious if I did it correctly, as it looks a bit off. There are people, who have a user account, and a role (teacher or student). they are participants in a class (where a class has many students and teachers; a student has many classes; a teacher has many classes). I think my class_instruction model is off in the DB, but please tell me if it will work, or if there is a better way (like maybe with a has_many_through table of participants)
schema.rb:
ActiveRecord::Schema.define(:version => 20130524160107) do
create_table "class_instructions", :force => true do |t|
t.string "name"
t.datetime "time"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "person_id"
end
add_index "class_instructions", ["person_id"], :name => "index_class_instructions_on_person_id"
create_table "people", :force => true do |t|
t.string "firstName"
t.string "lastName"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "people", ["user_id"], :name => "index_people_on_user_id"
create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "user_roles", :force => true do |t|
t.integer "user_id"
t.integer "role_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "user_roles", ["role_id"], :name => "index_user_roles_on_role_id"
add_index "user_roles", ["user_id"], :name => "index_user_roles_on_user_id"
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
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
My concern is that the person_id is part of the class. Is this correct?
ClassInsturction.rb :
class ClassInstruction < ActiveRecord::Base
belongs_to :people
has_many :cassignments
has_many :assignments, :through => :cassignments
def className
self.name
end
def classAssignments
return self.cassignments
end
end
I would make a joining table that has both class and people and use has many through. If people have many classes and classes have many people you can not do this any other way.
I have seen usually belongs_to with singular form:
belongs_to :person
I am not sure if this is what you are asking for though.