In Rails 4 I am trying to setup a polymorphic relationship where I have an address model and multiple other models will have one address. I have the following code:
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
end
class Corporate < ActiveRecord::Base
has_one :addressable
accepts_nested_attributes_for :addressable
end
In my DB, I have the following:
create_table "addresses", force: true do |t|
t.string "line1"
t.string "line2"
t.string "city"
t.string "zip_code"
t.string "contact_person"
t.string "contact_number"
t.integer "addressable_id"
t.string "addressable_type"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "corporates", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
However, when I do
#corporate = Corporate.new
#corporate.build_addressable
I get the following error:
NameError (uninitialized constant Corporate::Addressable)
Any idea what the issue is. I followed the rails guide, which had an example of has_many, while I used has_one.
Probably, should be:
class Corporate < ActiveRecord::Base
has_one :address, as: :addressable
end
Related
I have a User table and a Booking Table that is linked by a create_join_table what holds the user id and booking ids. When a user books a room, i need the id of both the user and new booking to go into that. I am getting the error above and im not sure why.
I have looked online and saw something similar, their class names were plural however I don't think I have that.
booking.rb
class Booking < ApplicationRecord
enum room_type: ["Basic Room", "Deluxe Room", "Super-Deluxe Room", "Piton Suite"]
has_many :join_tables
has_many :users, through: :join_tables
end
user.rb
class User < ApplicationRecord
has_secure_password
validates :email, format: {with: URI::MailTo::EMAIL_REGEXP}, presence: true, uniqueness: true
has_many :join_tables
has_many :bookings, through: :join_tables
end
join_table.rb
class JoinTable < ApplicationRecord
belongs_to :users
belongs_to :bookings
end
bookings_controller.rb
def create
#booking = Booking.create(booking_params)
current_user.bookings << #booking ##Where the error happens
db/schema
ActiveRecord::Schema.define(version: 2019_12_13_181019) do
create_table "bookings", force: :cascade do |t|
t.integer "room_type"
t.date "check_in"
t.date "check_out"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "join_tables", force: :cascade do |t|
t.integer "users_id"
t.integer "bookings_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["bookings_id"], name: "index_join_tables_on_bookings_id"
t.index ["users_id"], name: "index_join_tables_on_users_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
end
end
I have just tried to reproduce your problem and I have a similar exception
irb(main):003:0> User.first.bookings
NameError (uninitialized constant User::Bookings)
but, when I change
belongs_to :users
belongs_to :bookings
to
belongs_to :user
belongs_to :booking
in app/models/join_table.rb everything works as expected.
This is how I created the JoinTable model
$ rails generate model JoinTable
class CreateJoinTables < ActiveRecord::Migration[6.0]
def change
create_table :join_tables do |t|
t.references :user
t.references :booking
t.timestamps
end
end
end
As you can see in the belongs_to docs, it is used in the singular form most of the time.
I'm trying to return JSON API where a show action will
render json: user, include [:books, :friends, :comments]
Problem is, if I try to use the inverse_of in my User and Book model classes like this:
User Serializer
class UserSerializer < ActiveModel::Serializer
...
has_many :friends
has_many :books, inverse_of: :author
...
end
Book Serializer
class BookSerializer < ActiveModel::Serializer
...
belongs_to :author, class_name: "User", inverse_of: :books
...
end
I get an error:
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: books.user_id: SELECT "books".* FROM "books" WHERE "books"."user_id" = ?):
If I remove the inverse_of and has_many from my User serializer, then I don't get any errors, but then the JSON being returned does not contain the included association.
Likewise, the same happens between Comment and User models.
Am I doing something wrong ?
My DB Schema for my two models are:
User Schema
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "username"
t.string "email"
t.string "password_digest"
t.boolean "banned"
t.integer "role_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "photo"
t.boolean "email_confirmed", default: false
t.string "confirm_token"
t.string "password_reset_token"
t.boolean "show_private_info", default: false
t.boolean "show_contact_info", default: false
t.index ["role_id"], name: "index_users_on_role_id"
end
Book Schema
create_table "books", force: :cascade do |t|
t.string "title"
t.boolean "adult_content"
t.integer "author_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "published"
t.string "cover"
t.text "blurb"
t.index ["author_id"], name: "index_books_on_author_id"
end
When I went to generate my Book model with:
rails generate model books ... author:references
It created this migration file:
class CreateBooks < ActiveRecord::Migration[5.0]
def change
create_table :books do |t|
t.string :title
t.boolean :adult_content
t.references :author, foreign_key: true
t.timestamps
end
end
end
I assume that includes the necessary foreign key setup...
Try to change this line in your User model(user.rb):
has_many :books, inverse_of: :author
to
has_many :books, inverse_of: :author, foreign_key: :author_id
You need to tell rails what foreign_key you used if it's not the default one.And the association should be declared in your models, not serializers. In serializer you are adding keys by "has_many", inverse_of does't works here.
I have a Company that has many Users through a join table company_user. Each user should work for only one Company. This is a 1 to many relationship.
I have looked around for this and found the solution in https://stackoverflow.com/a/7080017/883102
But I get the error
PG::UndefinedTable: ERROR: relation "companies" does not exist
LINE 5: WHERE a.attrelid = '"companies"'::regclass
When I try to create a Company. How can I solve this?
My models are
Company
class Company < ActiveRecord::Base
has_many :employments
has_many :users, :through => :employments
end
Users
class User < ActiveRecord::Base
...
end
Employment
class Employment < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
The migration for my join table is
create_table :employment do |t|
t.belongs_to :company
t.belongs_to :user
t.timestamps
end
My schema.rb
create_table "company", force: true do |t|
t.integer "rating"
t.integer "phone"
t.string "name"
t.string "address"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "employment", id: false, force: true do |t|
t.integer "company_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.string "remember_token"
t.string "role"
end
Hi I found the answer here
https://stackoverflow.com/a/24318236/883102
The problem was that my table names were in the singular form, I changed these in the migration and then re-created the database. It all seems to be working fine now.
My User class ended up as
class User < ActiveRecord::Base
has_one :employment
has_one :company, :through => :employment
end
This was to allow bi-directional associations
I think this is a fairly common issue but I couldn't find a solution. My domain has two models, Company and ExternalLink (a representation of a social media provider like Facebook or Twitter). The join model, CompaniesExternalLink, has the direct URL to a social media account (ie http://www.facebook.com/goldmansachs). I need to validate the presence of that URL, but the join is created on the association, which breaks and returns ActiveRecord::RecordInvalid
Any advice would be great. Sample code below:
class Company < ActiveRecord::Base
has_many :companies_external_links
has_many :external_links, through: :companies_external_links, dependent: :destroy
end
class ExternalLink < ActiveRecord::Base
has_many :companies_external_links
has_many :companies, through: :companies_external_links, dependent: :destroy
end
class CompaniesExternalLink < ActiveRecord::Base
validates :url, presence: true
belongs_to :company
belongs_to :external_link
validates :external_link, uniqueness: {scope: :company_id}
end
The relevant parts of the schema.rb
create_table "companies", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
t.string "state"
t.string "logo"
t.string "slug"
t.text "description"
end
create_table "companies_external_links", force: true do |t|
t.integer "company_id"
t.integer "external_link_id"
t.string "url"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "external_links", force: true do |t|
t.string "source"
t.datetime "created_at"
t.datetime "updated_at"
t.string "icon"
end
I have a couple of objects in a Rails app ("Ticket", and "Comment")
class Ticket < ActiveRecord::Base
has_many :attributes
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :ticket
belongs_to :user
end
with the following schema:
create_table "comments", :force => true do |t|
t.integer "ticket_id"
t.integer "user_id"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "tickets", :force => true do |t|
t.integer "site_id"
t.integer "status"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
However, for some reason - whenever I do a #lead.comments I get a crash:
can't convert String into Integer
Any ideas? This is driving me nuts!
I think the line that's causing you pronlems is:
has_many :attributes
"attributes" is a special word in an Active Record. It refers to the values of the columns in the db.
If you try and override this with an association, then you will have problems.
My suggestion is that you should not have a model called an "attribute" - call it something else, eg "properties", and the problems will go away.