Вопросы по тегу: activerecord

(3)

Change many to many polymorphics association to one to many

User and campaign has many to many association with occupation.I want to change occupation association with user many to one. How I modify it? User has_many :common_occupations, :as => :profession has_many :occupations, :through => :common_occupations Occupation class Occupation :common_occupations, :source => :profession, :source_type => "User" has_many :campaigns, :through => :common_occupations, :source => :profession, :source_type => "Campaign" has_many :common_occupations Campaign has_many :common_occupations, :as => :profession has_many :occupations, :through => :common_occupations CommonOccupation belongs_to :occupation belongs_to :profession, :polymorphic => true
H

Haseeb Ahmad

6 лет назад

Ответов: 2

Несоответствие типов при заполнении базы данных

Я пытаюсь заполнить базу данных приложения, но получаю следующую ошибку: ActiveRecord::AssociationTypeMismatch: Role(#97332160) expected, got Fixnum(#76482890) Вот часть моего schema.rb, касающаяся двух таблиц, связанных с проблемой:create_table "roles", force: :cascade do |t| t.string "role", limit: 50, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "first_name", limit: 100, null: false t.string "surname", limit: 100, null: false t.string "email", limit: 255 t.integer "role_id", limit: 11 t.string "password", limit: 150 t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "users", ["first_name"], name: "index_users_on_first_name", using: :btree add_index "users", ["role_id"], name: "index_users_on_role_id", using: :btree add_index "users", ["surname"], name: "index_users_on_surname", using: :btree add_foreign_key "users", "roles" А вот и мои seed.rb команды:rl = Role.create(role: "root") User.create(first_name: "Edvaldo", surname: "Silva de Almeida Júnior", email: "edvaldo@my.domain.com", role_id: rl.id, password: "my_password") rl = Role.create(role: "admin") User.create(first_name: "Daiely", surname: "Fanchin", email: "daiely@my.domain.com", role_id: rl.id, password: "other_password") rl = Role.create(role: "user") User.create(first_name: "César", surname: "Silva", email: "cesar@my.domain.com", role_id: rl.id, password: "yet_other_password") Я нашел вопрос, где в принятом ответе предлагалось сделать что-то вроде:rl = Role.create(role: "root") User.create(first_name: "Edvaldo", surname: "Silva de Almeida Júnior", email: "edvaldo@my.domain.com", role_id: Role.find(rl.id), password: "my_password") Я пробовал, но получил ту же ошибку! Кроме того, я использую role_id как ассоциацию, а не саму роль. Итак, насколько я понимаю, он должен получить Fixnum, а не Role.
E

Ed de Almeida

6 лет назад

Ответов: 3

Parent id is not set while updating parent

I have two models class Parent and class Child parent.attribute_one errors.add(:base, "error message") end end end in my parent controller i have def update @parent = Parent.find(params[:id]) @parent.update_attributes(parent_params) #here is problem end private def trasnfer_params params.require(:parent).permit(:attribute_one, :attribute_two, child_attributes:[:parent_id, :attribute_one]) end when i check for parent_id in custom_validation it returns me nil and got nil class error for parent.attribute_one as parent is nil what am I doing wrong here. Here is the backtrace NoMethodError (undefined method `attribute_one' for nil:NilClass): app/models/child.rb:8:in `custom_validation' app/controllers/parents_controller.rb:12:in `update' Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.6ms) Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.6ms) Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.8ms) Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (19.6ms) Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/_markup.html (0.4ms) Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/style.css within layouts/inlined_string (0.5ms) Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/_inner_console_markup.html within layouts/inlined_string (0.4ms) Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/_prompt_box_markup.html within layouts/inlined_string (0.3ms) Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/console.js within layouts/javascript (17.9ms) Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/main.js within layouts/javascript (0.3ms) Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/error_page.js within layouts/javascript (0.4ms) Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/index.html (34.4ms)
A

Asnad Atta

6 лет назад

Ответов: 1

2022 WebDevInsider