I'm writing a demo to practice the gem of "carrierwave", and it worked all well in development ENV, but failed when I deployed to Heroku.
When I : heroku run rails s, get this error:
/app/app/models/article.rb:9:in `': uninitialized constant Article::PictureUploader (NameError)
There's my codes:
app/models/article.rb
class Article < ActiveRecord::Base
belongs_to :user
has_many :comments, dependent: :destroy
has_many :likes
validates :title, presence: true, length: {maximum: 50}
validates :content, length: {minimum: 10}
self.per_page = 10
attr_accessor :floor_number
mount_uploader :picture, PictureUploader
validate :picture_size
uploaders/picture_uploader.rb
class PictureUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process resize_to_limit: [600, 600]
storage :qiniu
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w{jpg gif png jpeg}
end
end
db/schema.rb
ActiveRecord::Schema.define(version: 20150529021048) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "content"
t.string "label"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "picture"
end
add_index "articles", ["user_id"], name: "index_articles_on_user_id"
create_table "comments", force: :cascade do |t|
t.text "content"
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
add_index "comments", ["article_id"], name: "index_comments_on_article_id"
add_index "comments", ["user_id"], name: "index_comments_on_user_id"
create_table "likes", force: :cascade do |t|
t.integer "likes_num"
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "likes", ["article_id"], name: "index_likes_on_article_id"
create_table "messages", force: :cascade do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "messages", ["user_id"], name: "index_messages_on_user_id"
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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "name"
t.boolean "admin"
t.datetime "created_at"
t.datetime "updated_at"
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
Error:
yesijiedeMacBook-Pro:blog killernova$ heroku run rails c
Running rails c attached to terminal... up, run.2077
/app/app/models/article.rb:9:in <class:Article>': uninitialized constant Article::PictureUploader (NameError)
from /app/app/models/article.rb:1:in'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:472:in block (2 levels) in eager_load!'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:471:ineach'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:471:in block in eager_load!'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:469:ineach'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:469:in eager_load!'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:346:ineager_load!'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/application/finisher.rb:56:in each'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/application/finisher.rb:56:inblock in '
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/initializable.rb:30:in instance_exec'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/initializable.rb:30:inrun'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/initializable.rb:55:in block in run_initializers'
from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:150:inblock in tsort_each'
from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:183:in block (2 levels) in each_strongly_connected_component'
from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:219:ineach_strongly_connected_component_from'
from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:182:in block in each_strongly_connected_component'
from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:180:ineach'
from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:180:in each_strongly_connected_component'
from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:148:intsort_each'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/initializable.rb:54:in run_initializers'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/application.rb:352:ininitialize!'
from /app/config/environment.rb:5:in <top (required)>'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/application.rb:328:inrequire_environment!'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:142:in require_application_and_environment!'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:67:inconsole'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in run_command!'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/commands.rb:17:in'
from /app/bin/rails:4:in require'
from /app/bin/rails:4:in'
yesijiedeMacBook-Pro:blog killernova$
:qiqiu is another gem "carrierwave-qiniu".
Everything is ok in localhost:3000, so anyone would like to tell me where am I wrong? Thanks!
github: https://github.com/killernova/blog
Have you commit your app/uploaders/ to heroku master?
I have same error, was forget to add those uploader files to remote repository
Related
I'm trying to deploy my app for the first time in heroku and I'm encountering a weird bug. When I try to run heroku rake db:migrate, i see this error :
rake aborted! StandardError: An error has occurred, this and all later
migrations canceled:
PG::UndefinedTable: ERROR: relation "companies" does not exist
: CREATE TABLE "users" ("id" bigserial primary key, "admin" boolean DEFAULT 'f', "admin_c" boolean DEFAULT 'f', "color" character varying, "initial" character varying, "name" character varying, "surname" character varying, "pseudo" character varying, "step" integer, "company_id" bigint, "email" character varying DEFAULT '' NOT NULL, "encrypted_password" character varying DEFAULT '' NOT NULL, "reset_password_token" character varying, "reset_password_sent_at" timestamp, "remember_created_at" timestamp, "sign_in_count" integer DEFAULT 0 NOT NULL, "current_sign_in_at" timestamp, "last_sign_in_at" timestamp, "current_sign_in_ip" character varying, "last_sign_in_ip" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_7682a3bdfe"
FOREIGN KEY ("company_id")
REFERENCES "companies" ("id")
)
I have verified a thousand times my migrations files Users & Companies but nothing seems bad. So I really don't know what to do. I also try all the database reset etc..
Like in this answer for example. But nothing is working for me. Anyone have seen this before ?? I'm really desperate at this point.
PS: And I also remove made the pg and sql3 changes.
So I give you my code if it can be useful to discover the problem
Company_model :
class Company < ApplicationRecord
has_many :users, dependent: :destroy
has_many :groups, dependent: :destroy
end
User_model :
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :company, optional: true
has_many :users_group, dependent: :destroy
has_many :groups, through: :users_group
has_many :users_post, dependent: :destroy
has_many :posts, through: :users_post
has_many :tasks_users, dependent: :destroy
has_many :tasks, through: :tasks_users
has_many :requests, dependent: :destroy
has_many :groupes_admin, dependent: :destroy
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :tasks, dependent: :destroy
end
Company migration :
class CreateCompanies < ActiveRecord::Migration[5.1]
def change
create_table :companies do |t|
t.string :name
t.string :ref
t.timestamps
end
end
end
Devise migration :
class DeviseCreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
## Database authenticatable
t.boolean :admin, default: false
t.boolean :admin_c, default: false
t.string :color
t.string :initial
t.string :name
t.string :surname
t.string :pseudo
t.integer :step
t.references :company, index: true, foreign_key: true
t.boolean :admin, default: false
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
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.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
end
end
Schema :
ActiveRecord::Schema.define(version: 20170727071936) do
create_table "comments", force: :cascade do |t|
t.string "content"
t.boolean "done"
t.integer "post_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["post_id"], name: "index_comments_on_post_id"
t.index ["user_id"], name: "index_comments_on_user_id"
end
create_table "companies", force: :cascade do |t|
t.string "name"
t.string "ref"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "groupes_admins", force: :cascade do |t|
t.integer "user_id"
t.integer "group_id"
t.index ["group_id"], name: "index_groupes_admins_on_group_id"
t.index ["user_id"], name: "index_groupes_admins_on_user_id"
end
create_table "groups", force: :cascade do |t|
t.string "name"
t.integer "cat"
t.boolean "main"
t.boolean "perso"
t.integer "effectif", default: 0
t.integer "elm", default: 0
t.integer "elm_d", default: 0
t.integer "date_cat"
t.integer "date_id"
t.datetime "date"
t.integer "company_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["company_id"], name: "index_groups_on_company_id"
end
create_table "posts", force: :cascade do |t|
t.text "content"
t.boolean "attached"
t.integer "attached_cat"
t.integer "attached_id"
t.boolean "done"
t.datetime "done_at"
t.integer "done_cat"
t.integer "donner_id"
t.datetime "upd_at"
t.integer "upd_cat"
t.integer "updater_id"
t.string "title"
t.date "deadline"
t.integer "group_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["group_id"], name: "index_posts_on_group_id"
t.index ["user_id"], name: "index_posts_on_user_id"
end
create_table "requests", force: :cascade do |t|
t.integer "user_id"
t.integer "group_id"
t.datetime "validate_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["group_id"], name: "index_requests_on_group_id"
t.index ["user_id"], name: "index_requests_on_user_id"
end
create_table "subtasks", force: :cascade do |t|
t.string "title"
t.datetime "date"
t.boolean "finished"
t.datetime "done_at"
t.integer "done_id"
t.integer "assign_id"
t.integer "task_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["task_id"], name: "index_subtasks_on_task_id"
end
create_table "tasks", force: :cascade do |t|
t.integer "user_id"
t.integer "group_id"
t.integer "post_id"
t.string "title"
t.datetime "date"
t.boolean "done"
t.integer "doner_id"
t.datetime "done_at"
t.boolean "assigned"
t.integer "elm", default: 0
t.integer "elm_d", default: 0
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["group_id"], name: "index_tasks_on_group_id"
t.index ["post_id"], name: "index_tasks_on_post_id"
t.index ["user_id"], name: "index_tasks_on_user_id"
end
create_table "tasks_users", force: :cascade do |t|
t.integer "task_id"
t.integer "user_id"
t.index ["task_id"], name: "index_tasks_users_on_task_id"
t.index ["user_id"], name: "index_tasks_users_on_user_id"
end
create_table "users", force: :cascade do |t|
t.boolean "admin", default: false
t.boolean "admin_c", default: false
t.string "color"
t.string "initial"
t.string "name"
t.string "surname"
t.string "pseudo"
t.integer "step"
t.integer "company_id"
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", null: false
t.datetime "updated_at", null: false
t.index ["company_id"], name: "index_users_on_company_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
create_table "users_groups", force: :cascade do |t|
t.integer "user_id"
t.integer "group_id"
t.index ["group_id"], name: "index_users_groups_on_group_id"
t.index ["user_id"], name: "index_users_groups_on_user_id"
end
create_table "users_posts", force: :cascade do |t|
t.integer "post_id"
t.integer "user_id"
t.index ["post_id"], name: "index_users_posts_on_post_id"
t.index ["user_id"], name: "index_users_posts_on_user_id"
end
end
The error message suggests that you're trying to create a reference to companies on the users table but the companies table doesn't yet exist. Try making sure the order of your migrations is such that companies is created before users. You can also try just loading the schema all at once rather than migrating one at a time, with heroku run rake db:schema:load.
DISCLAIMER: rake db:schema:load will wipe all of your data. It is best used for setting up a new database. Never run it against a production database that already contains critical data. Once you have data you will need to do incremental migrations.
Create the database first with rake db:create.
Hi I´m learning to use ActiveAdmin by building an rails App.
I've a products and a category model which are related Product belongs to category.
In the Admin Panel I can create Categories and Products
When I´m creating new product and assigning it to a category I have created the product always ends up belonging to a category called "No Category"
this "No category" option is supposed to be default if no other category is assigned to the product.
I´m not sure why this is happening since I can choose from multiple categories that I've created.
this is my product model
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :page
validates :title, :description, presence: true
validates :price_usd, :price_isl, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
has_attached_file :image, styles: { medium: "500x500#", thumb: "100x100#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
and this is my category.rb model
class Category < ActiveRecord::Base
has_many :products
end
And here is a copy of my Schema.rb
ActiveRecord::Schema.define(version: 20160826161114) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "active_admin_comments", force: :cascade do |t|
t.string "namespace"
t.text "body"
t.string "resource_id", null: false
t.string "resource_type", null: false
t.integer "author_id"
t.string "author_type"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id", using: :btree
add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace", using: :btree
add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id", using: :btree
create_table "admin_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
end
add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true, using: :btree
add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true, using: :btree
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "pages", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id", default: 5
end
add_index "pages", ["category_id"], name: "index_pages_on_category_id", using: :btree
create_table "products", force: :cascade do |t|
t.string "title"
t.string "image"
t.text "description"
t.decimal "price_usd"
t.decimal "price_isl"
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.integer "category_id", default: 4
end
add_index "products", ["category_id"], name: "index_products_on_category_id", using: :btree
add_foreign_key "pages", "categories"
add_foreign_key "products", "categories"
end
I've built a rails app and on pushing it up to heroku and running heroku run rake db:migrate i receive the error:
Migrating to CreateFollowingRelationships (20160615113231)
(0.4ms) BEGIN
== 20160615113231 CreateFollowingRelationships: migrating =====================
-- create_table(:following_relationships)
(7.1ms) CREATE TABLE "following_relationships" ("id" serial primary key, "follower_id" integer, "followed_user_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_39102b381e"
FOREIGN KEY ("follower_id")
REFERENCES "followers" ("id")
, CONSTRAINT "fk_rails_048c8f7cd9"
FOREIGN KEY ("followed_user_id")
REFERENCES "followed_users" ("id")
)
(0.5ms) ROLLBACK
(0.5ms) SELECT pg_advisory_unlock(2837140123622957145)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "followers" does not exist
: CREATE TABLE "following_relationships" ("id" serial primary key, "follower_id" integer, "followed_user_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_39102b381e"
FOREIGN KEY ("follower_id")
REFERENCES "followers" ("id")
, CONSTRAINT "fk_rails_048c8f7cd9"
FOREIGN KEY ("followed_user_id")
REFERENCES "followed_users" ("id")
)
The relationship I have setup is for users to follow users and be followed. It works perfectly in development. This is the first time i've had this error and can't find a solution. Please see the rest of my code below.
db/schema.rb
ActiveRecord::Schema.define(version: 20160618122126) do
create_table "chats", force: :cascade do |t|
t.integer "user_id"
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "profile_id"
t.index ["profile_id"], name: "index_chats_on_profile_id"
t.index ["user_id"], name: "index_chats_on_user_id"
end
create_table "comments", force: :cascade do |t|
t.integer "profile_id"
t.integer "chat_id"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["chat_id"], name: "index_comments_on_chat_id"
t.index ["profile_id"], name: "index_comments_on_profile_id"
end
create_table "following_relationships", force: :cascade do |t|
t.integer "follower_id"
t.integer "followed_user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["followed_user_id"], name: "index_following_relationships_on_followed_user_id"
t.index ["follower_id"], name: "index_following_relationships_on_follower_id"
end
create_table "locations", force: :cascade do |t|
t.string "locationable_type"
t.integer "locationable_id"
t.string "suburb"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "address"
t.string "state"
t.string "country"
t.string "postcode"
t.float "latitude"
t.float "longitude"
t.index ["locationable_type", "locationable_id"], name: "index_locations_on_locationable_type_and_locationable_id"
t.index ["user_id"], name: "index_locations_on_user_id"
end
create_table "orders", force: :cascade do |t|
t.integer "buyer_id"
t.integer "seller_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "posts", force: :cascade do |t|
t.integer "profile_id"
t.integer "user_id"
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["profile_id"], name: "index_posts_on_profile_id"
t.index ["user_id"], name: "index_posts_on_user_id"
end
create_table "profiles", force: :cascade do |t|
t.integer "user_id"
t.text "bio"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_profiles_on_user_id"
end
create_table "tutoring_relationships", force: :cascade do |t|
t.integer "tutor_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "tutee_id"
t.index ["tutee_id"], name: "index_tutoring_relationships_on_tutee_id"
t.index ["tutor_id"], name: "index_tutoring_relationships_on_tutor_id"
end
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.string "password_digest", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.boolean "tutor", default: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
create_table "votes", force: :cascade do |t|
t.string "votable_type"
t.integer "votable_id"
t.string "voter_type"
t.integer "voter_id"
t.boolean "vote_flag"
t.string "vote_scope"
t.integer "vote_weight"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope"
t.index ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope"
end
end
models/following_relationship.rb
class FollowingRelationship < ApplicationRecord
belongs_to :follower, class_name: 'User'
belongs_to :followed_user, class_name: 'User'
end
models/user.rb
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
has_many :chats
validates :email, presence: true, uniqueness: true
validates :password_digest, presence: true
has_many :follower_relationships,
foreign_key: :followed_user_id,
class_name: 'FollowingRelationship'
has_many :followers, through: :follower_relationships
has_many :followed_user_relationships,
foreign_key: :follower_id,
class_name: "FollowingRelationship"
has_many :followed_users, through: :followed_user_relationships
has_many :tutee_relationships,
foreign_key: :tutor_id,
class_name: 'TutoringRelationship'
has_many :tutees, through: :tutee_relationships
has_many :tutor_relationships,
foreign_key: :tutee_id,
class_name: 'TutoringRelationship'
has_many :tutors, through: :tutor_relationships
def following? user
followed_user_ids.include?(user.id)
end
def is_connected? user
tutee_ids.include?(user.id)
end
end
Please excuse the messy model, it would usually be in a helper method but for this post it's here. If anyone has any ideas please let me know! Thanks
I am new to rails. Everything is working fine locally but after deploying on heroku its giving me the above error.
Here is the posts_controllers action:
def new
#post = current_user.posts.build
end
and here is the schema.rb file content:
ActiveRecord::Schema.define(version: 20160516214156) do
create_table "comments", force: :cascade do |t|
t.text "comment"
t.integer "post_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
add_index "comments", ["user_id"], name: "index_comments_on_user_id"
create_table "posts", force: :cascade do |t|
t.text "description"
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.integer "user_id"
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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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
I had the same error.
To solve the problem, I specified :foreign_key of has_many relation.
My case:
class Assay::Attempt::Answer < ActiveRecord::Base
belongs_to :assay_attempt, class_name: 'Assay::Attempt'
end
class Assay::Attempt < ActiveRecord::Base
has_many :answers, foreign_key: :assay_attempt_id # there wasn't foreign_key option
end
I came across this as well on Rails 4.2.0, in my case everything seemed to be ok, since I had the foreign_key specified, what fixed the issue was when the foreign_key and foreign_type were specified as a string, not as a symbol:
has_many :answers, foreign_key: :assay_attempt_id #this was not working
has_many :answers, foreign_key: 'assay_attempt_id' #this was ok
I get the error with: heroku run rake db:migrate
Migrating to CreateResults (20150306174427)
== 20150306174427 CreateResults: migrating ====================================
-- create_table(:results)
-> 0.0479s
-- add_foreign_key(:results, :quantifieds, :users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
no implicit conversion of Symbol into Integer/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0.rc3/lib/active_record/connection_adapters/abstract/schema_statements.rb:707:in `[]'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0.rc3/lib/active_record/connection_adapters/abstract/schema_statements.rb:707:in `add_foreign_key'
I can't understand it because run rake db:migrate works.
class CreateResults < ActiveRecord::Migration
def change
create_table :results do |t|
t.string :result_value
t.date :date_value
t.belongs_to :quantified, index: true
t.references :user, index: true
t.timestamps null: false
end
add_foreign_key :results, :quantifieds, :users
add_index :results, [:user_id, :created_at]
end
end
class CreateQuantifieds < ActiveRecord::Migration
def change
create_table :quantifieds do |t|
t.string :categories
t.string :metric
t.references :user, index: true
t.timestamps null: false
end
add_foreign_key :quantifieds, :users
add_index :quantifieds, [:user_id, :created_at]
end
end
# 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: 20150306174427) do
create_table "days", force: true do |t|
t.integer "level_id"
t.integer "habit_id"
t.boolean "missed", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "goals", force: true do |t|
t.string "name"
t.date "deadline"
t.boolean "accomplished"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "goals", ["user_id", "created_at"], name: "index_goals_on_user_id_and_created_at"
add_index "goals", ["user_id"], name: "index_goals_on_user_id"
create_table "habits", force: true do |t|
t.datetime "left"
t.integer "level"
t.text "committed"
t.datetime "date_started"
t.string "trigger"
t.string "target"
t.string "reward"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "habits", ["user_id", "created_at"], name: "index_habits_on_user_id_and_created_at"
add_index "habits", ["user_id"], name: "index_habits_on_user_id"
create_table "levels", force: true do |t|
t.integer "user_id"
t.integer "habit_id"
t.integer "days_needed"
t.boolean "passed", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "quantifieds", force: true do |t|
t.string "categories"
t.string "metric"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "quantifieds", ["user_id", "created_at"], name: "index_quantifieds_on_user_id_and_created_at"
add_index "quantifieds", ["user_id"], name: "index_quantifieds_on_user_id"
create_table "relationships", force: true do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id"
add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id"
create_table "results", force: true do |t|
t.string "result_value"
t.date "date_value"
t.integer "quantified_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "results", ["quantified_id"], name: "index_results_on_quantified_id"
add_index "results", ["user_id", "created_at"], name: "index_results_on_user_id_and_created_at"
add_index "results", ["user_id"], name: "index_results_on_user_id"
create_table "taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", force: true do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "tags", ["name"], name: "index_tags_on_name", unique: true
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.text "missed_days"
t.text "missed_levels"
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.datetime "reset_sent_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
create_table "valuations", force: true do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "valuations", ["user_id", "created_at"], name: "index_valuations_on_user_id_and_created_at"
add_index "valuations", ["user_id"], name: "index_valuations_on_user_id"
end
UPDATE
Making these changes worked:
$ heroku pg:reset DATABASE
$ heroku rake db:migrate
class CreateResults < ActiveRecord::Migration
def change
create_table :results do |t|
t.string :result_value
t.date :date_value
t.integer :quantified_id
t.timestamps null: false
end
end
end
Thanks so much for your help! And for another perplexing question check this out: https://stackoverflow.com/questions/28863999/how-to-automate-into-method
t.belongs_to
Is invalid, you want either references or change it to integer and add _id to the field name. Then on your index add square brackets round the second and third arguments.