Create object on IRB - ruby-on-rails

I'm trying to create an object "lending" on irb to test my database and table connections but I can't.
I have successfully if I specify the :customer_id => 1 on the creation command.
The customer_id field in the database table doesn't settled as NOTNULL.
Could anyone help me?
This is the command I'm trying and the error:
irb(main):004:0> emprestimo = Emprestimo.create(:valor => 10000.00, :qnt_parcelas => 10, :valor_parcelas => 1000.00, :banco => 'Bic', :corretora => 'milreais')
(0.2ms) BEGIN
(0.2ms) ROLLBACK
=> #<Emprestimo id: nil, cliente_id: nil, valor: 10000.0, qnt_parcelas: 10, valor_parcelas: 1000.0, data_emprestimo: nil, banco: "Bic", corretora: "milreais", created_at: nil, updated_at: nil>
My /db.schema.rb:
ActiveRecord::Schema.define(version: 20170208154641) do
create_table "clientes", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "nome", limit: 45, null: false
t.string "cpf", limit: 14, null: false
t.string "rg", limit: 15, null: false
t.string "matricula", limit: 20, null: false
t.string "senha", limit: 10
t.date "data_nasc"
t.string "orgao", limit: 30
t.string "tel", limit: 15, null: false
t.string "tel2", limit: 15
t.string "convenio", limit: 10, null: false
t.string "email", limit: 35
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "emprestimos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "cliente_id"
t.float "valor", limit: 24, null: false
t.integer "qnt_parcelas", limit: 3, null: false
t.float "valor_parcelas", limit: 24, null: false
t.date "data_emprestimo"
t.string "banco", limit: 40, null: false
t.string "corretora", limit: 40
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "enderecos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "cliente_id"
t.string "rua", limit: 45, null: false
t.bigint "numero", null: false
t.string "complemento", limit: 45, null: false
t.string "bairro", limit: 45, null: false
t.string "cidade", limit: 45, null: false
t.string "estado", limit: 2, null: false
t.string "cep", limit: 9, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "operadors", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "user", limit: 45
t.string "senha", limit: 6
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
My Cliente and Emprestimo models:
class Emprestimo < ApplicationRecord
belongs_to :cliente
end
class Cliente < ApplicationRecord
has_one :endereco
has_many :emprestimos
end
Thank you very much.

Actually, in Rails 5, belongs_to relationship now is required by default. It basically adds a presence validator in your foreing key.
You can disable this behavior adding optional: true as belongs_to argument, like this:
belongs_to :cliente, optional: true
From the docs:
4.1.2.11 :optional
If you set the :optional option to true, then the presence of the associated object won't be validated. By default, this option is set to false.

Related

is creating model from another model and copying its value considered as good practices?

lets says I have inventory model and inventory_transaction model which contain inventory_id and inventory_transfer model which contain many inventory_transaction and has one branch target
then inside the inventory transfer I have this method which create item model
def create_item(user_id)
self.decrement_inventory_transactions.each do |entry|
item = Item.includes(:inventory_transaction).where(branch_id: self.branch_id).where("inventory_transaction.inventory_id": entry.inventory_id).first
if item
self.increment_item_transactions.create!(
item: item,
approved_at: DateTime.current,
approved_by_id: user_id,
quantity: entry.quantity
)
else
Item.create!(branch_id: self.branch_id, inventory_transaction_id: entry.id)
end
end
end
see in this code if there is no item with the particular inventory id then create the item if the item with that particular inventory_id is exist then create increment_item_transaction by copying the quantity from the inventory_transfer qty
my schema.rb
create_table "inventories", force: :cascade do |t|
t.bigint "receive_order_entry_id", null: false
t.decimal "price_base", default: "0.0", null: false
t.decimal "price_sell", default: "0.0", null: false
end
create_table "inventory_transactions", force: :cascade do |t|
t.bigint "inventory_id", null: false
t.integer "quantity", default: 0, null: false
t.string "inventory_transaction_header_type", null: false
t.bigint "inventory_transaction_header_id", null: false
t.string "type"
t.datetime "approved_at"
t.bigint "approved_by_id"
end
create_table "inventory_transfers", force: :cascade do |t|
t.bigint "item_request_id"
t.bigint "user_id", null: false
t.bigint "cabang_id", null: false
t.bigint "received_by_id"
t.datetime "received_at"
t.bigint "approved_by_id"
end
create_table "items", force: :cascade do |t|
t.bigint "inventory_transaction_id", null: false
t.bigint "branch_id", null: false
end
create_table "item_transactions", force: :cascade do |t|
t.bigint "item_id", null: false
t.integer "quantity", default: 0, null: false
t.string "type"
t.datetime "approved_at"
t.bigint "approved_by_id"
t.string "item_transaction_header_type", null: false
t.bigint "item_transaction_header_id", null: false
t.decimal "transaction_price", default: "0.0", null: false
end

Associations for statistics

I'm new to RoR and I need some help with associations. I'm using rails 6.0.3.4 and ruby 2.7.0.
Users can create cases and cases belongs to a certain district. Districts belongs to a state. It has to be that way, because cases can't belongs to a state.
Now I want to show the number of cases for a certain diagnosis for each state. I have to use district, to get all the cases for a state. How should I build the where(...) condition?
<!-- State -->
<div class="card">
<div class="card-body">
<h5 class="card-title">State</h5>
<p><%= State.find(1).titel%> (<%= #diagnosis.cases.where(...).count %>)</p>
</div>
</div>
My Models
case.rb
class Case < ApplicationRecord
before_create :set_pseud
belongs_to :user
belongs_to :diagnosis
belongs_to :district
belongs_to :report, optional: true
end
district.rb
class District < ApplicationRecord
has_many :users
has_many :cases
has_many :reports
belongs_to :state
end
state.rb
class State < ApplicationRecord
has_many :districts
has_many :users
end
For better understanding my schema.rb:
ActiveRecord::Schema.define(version: 2021_02_11_140244) do
create_table "cases", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "gender"
t.date "birthdate"
t.string "place_of_residence"
t.string "diagnosis"
t.bigint "user_id"
t.datetime "confirmed_at"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6, null: false
t.bigint "diagnosis_id"
t.bigint "district_id"
t.bigint "report_id"
t.string "pseud"
t.index ["diagnosis_id"], name: "index_cases_on_diagnosis_id"
t.index ["district_id"], name: "index_cases_on_district_id"
t.index ["pseud"], name: "index_cases_on_pseud"
t.index ["report_id"], name: "index_cases_on_report_id"
t.index ["user_id"], name: "index_cases_on_user_id"
end
create_table "diagnoses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "illness"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "districts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name"
t.string "place"
t.integer "postal_code"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "state_id", null: false
t.index ["state_id"], name: "index_districts_on_state_id"
end
create_table "reports", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "district_id"
t.text "comment"
t.datetime "date"
t.bigint "user_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["district_id"], name: "index_reports_on_district_id"
t.index ["user_id"], name: "index_reports_on_user_id"
end
create_table "states", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "titel"
t.string "abbr"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", 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.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "role"
t.string "first_name"
t.string "last_name"
t.bigint "district_id"
t.bigint "state_id"
t.index ["district_id"], name: "index_users_on_district_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
t.index ["state_id"], name: "index_users_on_state_id"
end
end
You should add a further association into state:
has_many :cases, through: :districts
Rather than finding your state in the view, you should do that in the controller and pass it to the view in an instance variable:
#state = State.find(params[:id])
I've assumed you're using a show action here rather than manually coding the state ID for some reason.
You can then do something like this:
#state.cases.where(diagnoses: { id: #diagnosis.id }).count
Or if you prefer, you can skip the .id on #diagnosis:
#state.cases.where(diagnoses: { id: #diagnosis }).count

Can we have multiple foreign key in active record schema?

I have an schema that has person table, email table, phone table and address table.
Person model
class Person< ApplicationRecord
has_many :emails, dependent: :destroy
accepts_nested_attributes_for :emails
default_scope { order(created_at: :desc) }
end
Email Model
class Email< ApplicationRecord
belongs_to :Person
has_one :phone, dependent: :destroy
has_many :address, dependent: :destroy
accepts_nested_attributes_for :phone
accepts_nested_attributes_for :address
end
Phone Model
class Phone< ApplicationRecord
belongs_to :Email
end
Address Model
class Address < ApplicationRecord
belongs_to :Email
end
My schema looks like this
ActiveRecord::Schema.define(version: 2019_06_03_231058) do
create_table "emails", id: :string, limit: 36, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t|
t.boolean "deleted", default: false, null: false
t.datetime "created_at", precision: 3, null: false
t.datetime "updated_at", precision: 3, null: false
t.string "Person_id", limit: 36
t.index ["Person_id", "created_at"], name: "index_email_on_Person_id_and_created_at"
end
create_table "phones", id: :string, limit: 36, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t|
t.string "phone_id", limit: 36, null: false
t.boolean "deleted", default: false, null: false
t.datetime "created_at", precision: 3, null: false
t.datetime "updated_at", precision: 3, null: false
t.string "email_id", limit: 36
t.string "person_id", limit: 36
t.index ["email_id", "created_at"], name: "index_phone_on_email_id_and_created_at"
t.index ["person_id"], name: "fk_rails_7119a1d90f"
end
create_table "persons", id: :string, limit: 36, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t|
t.boolean "deleted", default: false, null: false
t.datetime "created_at", precision: 3, null: false
t.datetime "updated_at", precision: 3, null: false
t.index ["created_at"], name: "index_persons_on_created_at"
end
create_table "address", id: :string, limit: 36, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t|
t.string "address_id", limit: 36, null: false
t.boolean "deleted", default: false, null: false
t.datetime "created_at", precision: 3, null: false
t.datetime "updated_at", precision: 3, null: false
t.string "email_id", limit: 36
t.string "person_id", limit: 36
t.index ["email_id", "created_at"], name: "index_address_on_email_id_and_created_at"
t.index ["person_id"], name: "fk_rails_485c78b376"
end
add_foreign_key "emails", "persons"
add_foreign_key "phones", "emails"
add_foreign_key "phones", "persons"
add_foreign_key "address", "emails"
add_foreign_key "address", "persons"
end
So if you notice in my schema at very end I have 2 foreign keys for phones and address.
when I insert data email_id is placed in person table but not person_id.
I see same issue in address table as well where email_id is placed but not person_id
I am new to rails Active record concept when through online documentation couldn't really solve this issue any help is much appreciated.

rails rake db:migrate sqllite3 error

StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: near "ENGINE": syntax error: CREATE TABLE "adv_bgs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "adv_bg_type" varchar, "adv_bg_id" integer, "uploaded_file_file_name" varchar, "uploaded_file_content_type" varchar, "uploaded_file_file_size" integer, "uploaded_file_updated_at" datetime, "head" boolean DEFAULT 'f', "imageable_id" integer, "imageable_type" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8
/mnt/c/Users/direwolf/Documents/web/rails_projects/okar/db/migrate/20170425144835_init_schema.rb:4:in `up'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `load'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `<main>'
ActiveRecord::StatementInvalid: SQLite3::SQLException: near "ENGINE": syntax error: CREATE TABLE "adv_bgs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "adv_bg_type" varchar, "adv_bg_id" integer, "uploaded_file_file_name" varchar, "uploaded_file_content_type" varchar, "uploaded_file_file_size" integer, "uploaded_file_updated_at" datetime, "head" boolean DEFAULT 'f', "imageable_id" integer, "imageable_type" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8
/mnt/c/Users/direwolf/Documents/web/rails_projects/okar/db/migrate/20170425144835_init_schema.rb:4:in `up'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `load'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `<main>'
SQLite3::SQLException: near "ENGINE": syntax error
/mnt/c/Users/direwolf/Documents/web/rails_projects/okar/db/migrate/20170425144835_init_schema.rb:4:in `up'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `load'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
What Do I do?
20170425144835_init_schema.rb
class InitSchema < ActiveRecord::Migration
def up
create_table "adv_bgs", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_bg_type"
t.integer "adv_bg_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_bg_type", "adv_bg_id"], name: "index_adv_bgs_on_adv_bg_type_and_adv_bg_id", using: :btree
t.index ["head"], name: "index_adv_bgs_on_head", using: :btree
end
create_table "adv_logos", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_logo_type"
t.integer "adv_logo_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_logo_type", "adv_logo_id"], name: "index_adv_logos_on_adv_logo_type_and_adv_logo_id", using: :btree
t.index ["head"], name: "index_adv_logos_on_head", using: :btree
end
create_table "adv_pic1s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic1_type"
t.integer "adv_pic1_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic1_type", "adv_pic1_id"], name: "index_adv_pic1s_on_adv_pic1_type_and_adv_pic1_id", using: :btree
t.index ["head"], name: "index_adv_pic1s_on_head", using: :btree
end
create_table "adv_pic2s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic2_type"
t.integer "adv_pic2_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic2_type", "adv_pic2_id"], name: "index_adv_pic2s_on_adv_pic2_type_and_adv_pic2_id", using: :btree
t.index ["head"], name: "index_adv_pic2s_on_head", using: :btree
end
create_table "adv_pic3s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic3_type"
t.integer "adv_pic3_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic3_type", "adv_pic3_id"], name: "index_adv_pic3s_on_adv_pic3_type_and_adv_pic3_id", using: :btree
t.index ["head"], name: "index_adv_pic3s_on_head", using: :btree
end
create_table "adv_pic4s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic4_type"
t.integer "adv_pic4_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic4_type", "adv_pic4_id"], name: "index_adv_pic4s_on_adv_pic4_type_and_adv_pic4_id", using: :btree
t.index ["head"], name: "index_adv_pic4s_on_head", using: :btree
end
create_table "adv_pic5s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic5_type"
t.integer "adv_pic5_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic5_type", "adv_pic5_id"], name: "index_adv_pic5s_on_adv_pic5_type_and_adv_pic5_id", using: :btree
t.index ["head"], name: "index_adv_pic5s_on_head", using: :btree
end
create_table "adv_pic6s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic6_type"
t.integer "adv_pic6_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic6_type", "adv_pic6_id"], name: "index_adv_pic6s_on_adv_pic6_type_and_adv_pic6_id", using: :btree
t.index ["head"], name: "index_adv_pic6s_on_head", using: :btree
end
create_table "adv_pic7s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic7_type"
t.integer "adv_pic7_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic7_type", "adv_pic7_id"], name: "index_adv_pic7s_on_adv_pic7_type_and_adv_pic7_id", using: :btree
t.index ["head"], name: "index_adv_pic7s_on_head", using: :btree
end
create_table "advertising_applications", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.integer "user_id"
t.integer "anon_user_id"
t.integer "televisor_id", limit: 1, null: false
t.integer "advertising_public_status_id", limit: 1, default: 1
t.integer "advertising_period_id", limit: 1, default: 1
t.integer "payment_status_id", limit: 1, default: 1
t.integer "step", limit: 1, default: 1
t.integer "spec_id", limit: 1, default: 1
t.string "email"
t.string "lnk"
t.string "redwords"
t.string "zkname"
t.string "zkdescr"
t.integer "price"
t.integer "charge", limit: 1, default: 1
t.datetime "endtime", default: '2017-04-25 17:47:04'
t.text "comment", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["advertising_period_id"], name: "index_advertising_applications_on_advertising_period_id", using: :btree
t.index ["advertising_public_status_id"], name: "index_advertising_applications_on_advertising_public_status_id", using: :btree
t.index ["anon_user_id"], name: "index_advertising_applications_on_anon_user_id", using: :btree
t.index ["payment_status_id"], name: "index_advertising_applications_on_payment_status_id", using: :btree
t.index ["televisor_id"], name: "index_advertising_applications_on_televisor_id", using: :btree
t.index ["user_id"], name: "index_advertising_applications_on_user_id", using: :btree
end
create_table "advertising_periods", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "advertising_public_statuses", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "anon_saved_searches", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "subtype", limit: 3, null: false
t.string "link_to", limit: 64, null: false
t.string "name", limit: 32
t.string "category"
t.string "square_from", limit: 24
t.string "square_to", limit: 24
t.string "price_from", limit: 24
t.string "price_to", limit: 24
t.string "price_src", limit: 3
t.text "data", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "metro_data", limit: 65535, null: false
t.integer "add_params_count", limit: 1, unsigned: true
t.integer "main_city", limit: 1, unsigned: true
t.text "location_tags_data", limit: 65535, null: false
end
create_table "anon_users", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "email"
t.string "access_level", limit: 16, default: "anon", null: false
t.string "access_code", limit: 32, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_anon_users_on_email", using: :btree
end
create_table "area_types", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "avatars", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "imageable_type"
t.integer "imageable_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["imageable_type", "imageable_id"], name: "index_avatars_on_imageable_type_and_imageable_id", using: :btree
end
create_table "bad_room_kinds", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "bad_room_nums", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "bad_room_presents", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "balcons", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "bg_cn_names", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["value"], name: "ivalue", type: :fulltext
end
You are using MySQL-specific syntax ENGINE=MyISAM DEFAULT CHARSET=utf8 in SQLite database: SQLite3::SQLException: near "ENGINE":

Populate a thrid model, pulling data from different model with an after_create callback on User model

I have 3 models, User, member_role & member. I need to use after_create callback on User model and populate the member model fetching the role from member_role model.
Would appreciate any help regarding how I can accomplish that.
My Member_role model loos like
1 - admin
2 - teacher
3- student
4 - so on
And mr member Model looks like
member_id
user_id
member_role_id
Need to populate Member model with an after_create callback on User model.
Here is my Schema.rb
ActiveRecord::Schema.define(version: 20150930061155) do
create_table "member_roles", force: :cascade do |t|
t.string "role", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "members", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id", limit: 4
t.integer "member_role_id", limit: 4
end
create_table "users", force: :cascade do |t|
t.string "provider", limit: 255, default: "email", null: false
t.string "uid", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "reset_password_token", limit: 255
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", limit: 4, default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip", limit: 255
t.string "last_sign_in_ip", limit: 255
t.string "confirmation_token", limit: 255
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email", limit: 255
t.string "name", limit: 255
t.string "nickname", limit: 255
t.string "image", limit: 255
t.string "email", limit: 255
t.text "tokens", limit: 65535
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, using: :btree
end
Assuming the default MemberRole is the first one:
class User
after_create :assign_member_role
private
def assign_member_role
mr = MemberRole.first # default MR
Member.create!(user_id: self.id, member_role_id: mr.id)
end
end

Resources