Related
Server log screencap
Hi everyone!
Was writing a rating system for an airBnb style project so i made the objects Host and Guest as reference to the User object.
But something is wrong in my code:
SQLite3::SQLException: no such table: main.hosts
In fact the method looks for host table i dont have cause it should be associated to the Users one.
migration
class CreateReviews < ActiveRecord::Migration[6.0]
def change
create_table :reviews do |t|
t.text :comment
t.integer :star, default: 1
t.references :car, foreign_key: true
t.references :reservation, foreign_key: true
t.references :guest, foreign_key: true
t.references :host, foreign_key: true
t.string :type
t.timestamps
end
end
end
Schema
create_table "reviews", force: :cascade do |t|
t.text "comment"
t.integer "star", default: 1
t.integer "car_id"
t.integer "reservation_id"
t.integer "guest_id"
t.integer "host_id"
t.string "type"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["car_id"], name: "index_reviews_on_car_id"
t.index ["guest_id"], name: "index_reviews_on_guest_id"
t.index ["host_id"], name: "index_reviews_on_host_id"
t.index ["reservation_id"], name: "index_reviews_on_reservation_id"
Models:
class HostReview < Review
belongs_to :host, class_name: "User"
end
class User < ApplicationRecord
....
has_many :host_reviews, class_name: "HostReview", foreign_key: "host_id"
I think you can do something like this:
t.references :host, references: :users, foreign_key: true
or alternatively
t.integer :host_id
and then
add_foreign_key :reviews, :users, column: :host_id
Before I start, I should say I have already checked out Rails 5.1.: destroy records in "has_many: through" association with restriction and has_many through association dependent destroy under condition of who called destroy without results.
My app consist in USERS that has_one EMPRESA.
An EMPRESA may have several TAGS
A TAG may have several EMPRESAS (To do this I have used has_many :through)
My case: I got this screen error:
And I know the origin of this error is because I'm trying to destroy items with pending references. But I can't identify th issue.
By looking at server console I can guess the problem involve empresa, tag, and tagging.
Models involved
class Empresa < ApplicationRecord
skip_callback :validate, after: :create
after_initialize :set_default_plan, :if => :new_record?
attr_accessor :tag_list
enum plan: [:noplan, :basic, :plus, :premium]
belongs_to :user
belongs_to :category, optional: true
has_many :promos, dependent: :destroy
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
mount_uploader :logo, LogoUploader
mount_uploaders :fotos, FotosUploader
def tag_list
tags.join(", ")
end
def tag_list=(names)
tag_names = names.split(",").collect {|str| str.strip.downcase}.uniq
new_or_existing_tags = tag_names.collect {|tag_name| Tag.find_or_create_by(name: tag_name)}
self.tags = new_or_existing_tags
end
def set_default_plan
self.plan ||= :noplan
end
end
class Tag < ApplicationRecord
has_many :empresas, through: :taggings
has_many :taggings, dependent: :destroy
def to_s
name
end
end
class Tagging < ApplicationRecord
belongs_to :empresa
belongs_to :tag
end
class Category < ApplicationRecord
validates :name, presence: true, length:{ minimum: 3 }, uniqueness: true
has_many :empresas, dependent: :nullify
end
class User < ApplicationRecord
enum role: [:user, :editor, :admin, :superadmin]
after_initialize :set_default_role, :if => :new_record?
has_one :empresa, dependent: :destroy
has_many :incidents, dependent: :destroy
has_many :comments, dependent: :destroy
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
def set_default_role
self.role ||= :user
end
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
schema.rb (irrelevant tables removed)
ActiveRecord::Schema.define(version: 20180531033550) do
enable_extension "plpgsql"
create_table "empresas", force: :cascade do |t|
t.string "logo"
t.string "name"
t.text "description"
t.text "excerpt"
t.string "address"
t.string "web"
t.string "email"
t.string "tel"
t.string "video"
t.json "fotos"
t.integer "plan", default: 0
t.float "mlon"
t.float "mlat"
t.string "schedule0"
t.string "schedule1"
t.string "schedule2"
t.string "schedule3"
t.string "schedule4"
t.string "schedule5"
t.string "schedule6"
t.string "schedule7"
t.string "schedule8"
t.string "schedule9"
t.string "schedule10"
t.string "schedule11"
t.string "schedule12"
t.string "schedule13"
t.string "schedule14"
t.string "schedule15"
t.string "schedule16"
t.string "schedule17"
t.string "schedule18"
t.string "schedule19"
t.string "schedule20"
t.string "schedule21"
t.string "schedule22"
t.string "schedule23"
t.string "schedule24"
t.string "schedule25"
t.string "schedule26"
t.string "schedule27"
t.integer "tag_id"
t.integer "offer_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
t.index ["category_id"], name: "index_empresas_on_category_id", using: :btree
end
create_table "taggings", force: :cascade do |t|
t.integer "empresa_id"
t.integer "tag_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["empresa_id"], name: "index_taggings_on_empresa_id", using: :btree
t.index ["tag_id"], name: "index_taggings_on_tag_id", using: :btree
end
create_table "tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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.integer "creditos", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "empresa_id"
t.integer "role"
t.string "first_name"
t.string "last_name"
t.date "birthdate"
t.string "dni"
t.string "phone"
t.string "address"
t.string "gender"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["empresa_id"], name: "index_users_on_empresa_id", using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
add_foreign_key "comments", "incidents"
add_foreign_key "comments", "users"
add_foreign_key "incidents", "users"
add_foreign_key "promos", "empresas"
add_foreign_key "taggings", "empresas"
add_foreign_key "taggings", "tags"
add_foreign_key "users", "empresas"
end
You do not need empresa_id in users.
Remove the referential integrity and column via:
rails g migration remove_constraint_from_users
Edit the newly created migration file to add the following in def change block
def change
remove_foreign_key :users, :empresas
remove_column :users, :empresa_id
end
I understand the cause of a Stack Level Too Deep error. I am failing to spot where/why it is occurring in my code base.
I've implemented a multi-model, multi-step wizard. The first two models (User and Company) are working, it is when I attempt to add in the third (Address) I get the error.
I suspect the error is related to the associations between the models, although I've failed to debug.
The code snippets below function correctly except when I add the 3 lines (marked with comments in the snippet) too the file app/wizards/user_wizard/step1.rb.
Relevant Models
app/models/company.rb
class Company < ActiveRecord::Base
include Validatable::Company
# Associations:
has_many :addresses, inverse_of: :company
accepts_nested_attributes_for :addresses, reject_if: :all_blank
has_many :employees, inverse_of: :company
accepts_nested_attributes_for :employees, reject_if: :all_blank
has_many :licenses, inverse_of: :company
accepts_nested_attributes_for :licenses, reject_if: :all_blank
has_many :vehicles, inverse_of: :company
accepts_nested_attributes_for :vehicles, reject_if: :all_blank
has_one :user, inverse_of: :company
end
app/models/address.rb
class Address < ActiveRecord::Base
# Associations:
belongs_to :company, inverse_of: :addresses
has_many :licenses, inverse_of: :address
accepts_nested_attributes_for :licenses, reject_if: :all_blank
has_many :initial_analyses, inverse_of: :address
accepts_nested_attributes_for :initial_analyses, reject_if: :all_blank
end
app/models/user.rb
class User < ActiveRecord::Base
include SoftDeletable
include Validatable::User
# Constants:
MARKER_ATTRIBUTES = %w[user_name].freeze # get marked with '(deleted)'
DEPENDANT_CHILDREN = %w[none].freeze # child resources to be deleted
# Associations:
belongs_to :role, inverse_of: :users
belongs_to :company, inverse_of: :user
accepts_nested_attributes_for :company, reject_if: :all_blank
has_many :auto_quotes, inverse_of: :user
end
db/schema.rb
ActiveRecord::Schema.define(version: 20170616131833) do
create_table "addresses", force: :cascade do |t|
t.integer "company_id"
t.text "site_name"
t.string "premises_code"
t.string "exempt_premises_code"
t.text "address"
t.string "city"
t.string "county"
t.string "sic_code"
t.string "postcode"
t.string "country"
t.boolean "sic_update"
t.boolean "deleted", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
end
create_table "companies", force: :cascade do |t|
t.string "company_name"
t.string "registration_number"
t.string "type_of_business"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
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.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts", default: 0, null: false
t.string "unlock_token"
t.datetime "locked_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "user_name"
t.datetime "deleted_at"
t.integer "role_id"
t.integer "company_id"
t.string "invitation_token"
t.datetime "invitation_created_at"
t.datetime "invitation_sent_at"
t.datetime "invitation_accepted_at"
t.integer "invitation_limit"
t.integer "invited_by_id"
t.string "invited_by_type"
t.integer "invitations_count", default: 0
end
add_index "users", ["company_id"], name: "index_users_on_company_id", unique: true
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["invitation_token"], name: "index_users_on_invitation_token", unique: true
add_index "users", ["invitations_count"], name: "index_users_on_invitations_count"
add_index "users", ["invited_by_id"], name: "index_users_on_invited_by_id"
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
add_index "users", ["unlock_token"], name: "index_users_on_unlock_token", unique: true
end
Wizards
app/wizards/user_wizard/base.rb
module UserWizard
class Base
include ActiveModel::Model
STEPS = %w[step1 step2].freeze
attr_accessor :user
delegate(*::User.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :user)
def initialize(user_attributes)
#user = ::User.new(user_attributes)
end
end
end
app/wizards/user_wizard/step1.rb
module UserWizard
class Step1 < UserWizard::Base
include Validatable::Company
attr_accessor :company
# One of 3 lines triggering circular reference by adding in Address model
attr_accessor :address
delegate(*::Company.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :company)
# One of 3 lines triggering circular reference by adding in Address model
delegate(*::Address.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :address)
def initialize(user_attributes)
super
#company = #user.build_company
# One of 3 lines triggering circular reference by adding in Address model
#address = #user.company.addresses.build
end
end
end
app/wizards/user_wizard/step2.rb
ommitted as it is irrelevant. code fails before ever instantiating this class
address has an address attribute. So the delegate method is trying to create a method address that will be delegated to address.
I'd suggest this:
module UserWizard
class Step1 < UserWizard::Base
include Validatable::Company
attr_accessor :company
# One of 3 lines triggering circular reference by adding in Address model
attr_accessor :company_address
delegate(*::Company.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :company)
# One of 3 lines triggering circular reference by adding in Address model
delegate(*::Address.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :company_address)
def initialize(user_attributes)
super
#company = #user.build_company
# One of 3 lines triggering circular reference by adding in Address model
#company_address = #user.company.addresses.build
end
end
end
I want that if a user has ordered a meal, he can talk to the meal provider... and vice versa...
Users can provide meals and order meals too, like on air bnb you can be a guest or a host (or both)
Exemples:
Lets say we have Bob(buyer) who orders a meal from John(maker) I want that John and Bob can chat together
John(buyer) orders a meal from Mike(maker), John and Mike can chat together.
conversations/index.html.erb
<% #users.each do |user| %>
<%= link_to user.full_name , conversations_path(user_id: user), remote: true, method: :post %>
<% end %>
user.rb (I have doubt in this model...)
has_many :meals #Meals that the user offers
has_many :received_orders, class_name: "Order" #The user receive an order
has_many :placed_orders, through: :meals, class_name: "Order" #The user order a meal
has_many :prepared_orders, through: :received_orders, class_name: "Meal", source: :meal #The user has prepared the order
####maybe this way below....TODO
# has_many :meals
# has_many :orders_as_a_customer, class_name: "Order"# same as: has_many :orders
# has_many :orders_as_a_seller, through: :orders_as_a_customer, class_name: "Meal", source: :meal
# has_many :orders, through: :meals
order.rb
class Order < ApplicationRecord
before_save :calculate_price
belongs_to :user
belongs_to :meal
has_many :notifications, as: :topic
monetize :amount_cents, as: :amount
def payment
self.payment_status = true
self.save
end
def calculate_price
self.amount = (self.quantity * meal.price)
end
end
Well I've followed this tutorial to create my chat, and I bet it's possible to adjust to my app to it...
conversation.rb
class Conversation < ApplicationRecord
has_many :messages, dependent: :destroy
belongs_to :sender, foreign_key: :sender_id, class_name: "User"
belongs_to :recipient, foreign_key: :recipient_id, class_name: "User"
validates :sender_id, uniqueness: { scope: :recipient_id }
scope :between, -> (sender_id, recipient_id) do
where(sender_id: sender_id, recipient_id: recipient_id).or(
where(sender_id: recipient_id, recipient_id: sender_id)
)
end
def self.get(sender_id, recipient_id)
conversation = between(sender_id, recipient_id).first
return conversation if conversation.present?
create(sender_id: sender_id, recipient_id: recipient_id)
end
def opposed_user(user)
user == recipient ? sender : recipient
end
end
conversations_controller.rb
class ConversationsController < ApplicationController
def create
#conversation = Conversation.get(current_user.id, params[:user_id])
add_to_conversations unless conversated?
respond_to do |format|
format.js
end
end
def index
session[:conversations] ||= []
#users = User.all.where.not(id: current_user)
#conversations = Conversation.includes(:recipient, :messages).find(session[:conversations])
end
def close
#conversation = Conversation.find(params[:id])
session[:conversations].delete(#conversation.id)
respond_to do |format|
format.js
end
end
private
def add_to_conversations
session[:conversations] ||= []
session[:conversations] << #conversation.id
end
def conversated?
session[:conversations].include?(#conversation.id)
end
end
my actual schema.rb
ActiveRecord::Schema.define(version: 20170629192651) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "attachinary_files", force: :cascade do |t|
t.string "attachinariable_type"
t.integer "attachinariable_id"
t.string "scope"
t.string "public_id"
t.string "version"
t.integer "width"
t.integer "height"
t.string "format"
t.string "resource_type"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["attachinariable_type", "attachinariable_id", "scope"], name: "by_scoped_parent", using: :btree
end
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 "conversations", force: :cascade do |t|
t.integer "recipient_id"
t.integer "sender_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "order_id"
t.index ["order_id"], name: "index_conversations_on_order_id", using: :btree
t.index ["recipient_id", "sender_id"], name: "index_conversations_on_recipient_id_and_sender_id", unique: true, using: :btree
end
create_table "ingredients", force: :cascade do |t|
t.string "name"
t.integer "meal_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["meal_id"], name: "index_ingredients_on_meal_id", using: :btree
end
create_table "meals", force: :cascade do |t|
t.string "menu_name"
t.integer "portion"
t.date "availability"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "category_id"
t.string "images"
t.string "location"
t.float "latitude"
t.float "longitude"
t.integer "price"
t.index ["user_id"], name: "index_meals_on_user_id", using: :btree
end
create_table "messages", force: :cascade do |t|
t.text "body"
t.integer "user_id"
t.integer "conversation_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["conversation_id"], name: "index_messages_on_conversation_id", using: :btree
t.index ["user_id"], name: "index_messages_on_user_id", using: :btree
end
create_table "notifications", force: :cascade do |t|
t.boolean "read", default: false
t.string "content"
t.integer "user_id"
t.string "topic_type"
t.integer "topic_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "order_id"
t.index ["order_id"], name: "index_notifications_on_order_id", using: :btree
t.index ["topic_type", "topic_id"], name: "index_notifications_on_topic_type_and_topic_id", using: :btree
t.index ["user_id"], name: "index_notifications_on_user_id", using: :btree
end
create_table "orders", force: :cascade do |t|
t.text "message"
t.boolean "payment_status", default: false
t.integer "quantity"
t.integer "user_id"
t.integer "meal_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "amount_cents", default: 0, null: false
t.json "payment"
t.index ["meal_id"], name: "index_orders_on_meal_id", using: :btree
t.index ["user_id"], name: "index_orders_on_user_id", using: :btree
end
create_table "reviews", force: :cascade do |t|
t.integer "rating"
t.text "comment"
t.integer "meal_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["meal_id"], name: "index_reviews_on_meal_id", using: :btree
t.index ["user_id"], name: "index_reviews_on_user_id", using: :btree
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 "provider"
t.string "uid"
t.string "facebook_picture_url"
t.string "first_name"
t.string "last_name"
t.string "token"
t.datetime "token_expiry"
t.string "nickname"
t.string "avatar"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
add_foreign_key "conversations", "orders"
add_foreign_key "meals", "users"
add_foreign_key "messages", "conversations"
add_foreign_key "messages", "users"
add_foreign_key "notifications", "orders"
add_foreign_key "notifications", "users"
add_foreign_key "orders", "meals"
add_foreign_key "orders", "users"
add_foreign_key "reviews", "meals"
add_foreign_key "reviews", "users"
end
Concluding from the comments, you'd need to have different models. Here are the ones that make sense to me:
class User < ApplicationRecord
has_many :meals
has_many :orders
has_many :sent_messages, :class => 'ChatMessage', :foreign_key => 'sender_id'
has_many :received_messages, :class => 'ChatMessage', :foreign_key => 'receiver_id'
end
class Meal < ApplicationRecord
belongs_to :user
belongs_to :order_item
end
class Order < ApplicationRecord
belongs_to :user
has_many :order_items
end
class OrderItem < ApplicationRecord
belongs_to :order
has_one :meal
end
class ChatMessage < ApplicationRecord
belongs_to :sender, :class => 'User'
belongs_to :receiver, :class => 'User'
end
I followed the documentation, manually creating the migration file and the models. I already had a User model in my app:
user.rb:
class User < ActiveRecord::Base
authenticates_with_sorcery!
validates :name, presence: true
validates :password, length: { minimum: 4 }
validates :password, confirmation: true
validates :password_confirmation, presence: true
validates :email, uniqueness: true
has_many :testimonials
has_many :materials
groupify :group_member
groupify :named_group_member
end
class Assignment < ActiveRecord::Base
groupify :group_member
end
group.rb:
class Group < ActiveRecord::Base
groupify :group, members: [:users, :assignments], default_members: :users
end
GroupMembership.rb:
class GroupMembership < ActiveRecord::Base
groupify :group_membership
end
schema.rb:
ActiveRecord::Schema.define(version: 20150301133633) do
create_table "group_memberships", force: :cascade do |t|
t.string "member_type"
t.integer "member_id"
t.integer "group_id"
t.string "group_name"
t.string "membership_type"
end
add_index "group_memberships", ["group_id"], name: "index_group_memberships_on_group_id"
add_index "group_memberships", ["group_name"], name: "index_group_memberships_on_group_name"
add_index "group_memberships", ["member_id", "member_type"], name: "index_group_memberships_on_member_id_and_member_type"
create_table "groups", force: :cascade do |t|
t.string "type"
end
create_table "materials", force: :cascade do |t|
t.string "level"
t.text "description"
t.string "link"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.integer "user_id"
end
add_index "materials", ["user_id"], name: "index_materials_on_user_id"
create_table "testimonials", 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 "testimonials", ["user_id", "created_at"], name: "index_testimonials_on_user_id_and_created_at"
add_index "testimonials", ["user_id"], name: "index_testimonials_on_user_id"
create_table "tests", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.string "crypted_password"
t.string "salt"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
I run my console in sandbox mode and do:
group = Group.new
user = User.new
group.add user
And my console spews out:
NameError: uninitialized constant User::GroupMembership
What's going on?
SOLVED: The class GroupMembership.rb is written in camel case, so the filename needs to be:
group_membership.rb