Rails 4 and PostgreSQL Arrays:
Can anybody solve the bellow problem
UserModel:
class User < ActiveRecord::Base
has_many :courses, dependent: :destroy
end
Course Model:
class Course < ActiveRecord::Base
belongs_to :user
end
def course_params
params.require(:course).permit(:name, …, user_id: [])
end
Course/ _form.html.erb file contains:
<%= hidden_field_tag(:user_id, @course.user_id) %>
class CreateCourses < ActiveRecord::Migration
def change
create_table :courses do |t|
t.integer :user_id, :array => true
t.text :name, default: " "
t.text :description, default: " "
t.text :welcome_note, default: " "
t.text :promotional_text, default: " "
t.string :phone_number, default: " "
t.string :restaurant_phone_number, default: " "
t.timestamps
end
end
end
…
Started POST “/courses” for 127.0.0.1 at 2015-02-24 17:07:07 +0530
Processing by CoursesController#create as HTML
Parameters: {“utf8”=>“✓”,
“authenticity_token”=>“vXo+MeMi0TedrcIO3F+m2X6uMDHuH/deDvj3yKbtuBc=”,
“course”=>{“name”=>" kvanadev6", “line1”=>" “, “line2”=>” “, “city”=>”
“, “state”=>” “, “zip”=>” “, “latitude”=>“0.0”, “longitude”=>“0.0”,
“description”=>” “, “welcome_note”=>” “, “promotional_text”=>” “,
“phone_number”=>” “, “restaurant_phone_number”=>” “}, “commit”=>“Create
Course”}
User Load (0.4ms) SELECT “users”.* FROM “users” WHERE “users”.“id”
= 2 ORDER BY “users”.“id” ASC LIMIT 1
Course: #<Course id: nil, user_id: 2, name: " kvanadev6”, description: "
", welcome_note: " ", promotional_text: " ", phone_number: " ",
restaurant_phone_number: " ", created_at: nil, updated_at: nil,
main_image_file_name: nil, main_image_content_type: nil,
main_image_file_size: nil, main_image_updated_at: nil,
restaurant_menu_image_file_name: nil,
restaurant_menu_image_content_type: nil,
restaurant_menu_image_file_size: nil, restaurant_menu_image_updated_at:
nil, line1: " ", line2: " ", city: " ", state: " ", zip: " “, latitude:
0.0, longitude: 0.0>
(0.4ms) BEGIN
SQL (1.3ms) INSERT INTO “courses” (“created_at”, “name”,
“updated_at”, “user_id”) VALUES ($1, $2, $3, $4) RETURNING “id”
[[“created_at”, “2015-02-24 11:37:07.762549”], [“name”, " kvanadev6”],
[“updated_at”, “2015-02-24 11:37:07.762549”], [“user_id”, 2]]
PG::InvalidTextRepresentation: ERROR: malformed array literal: “2”
DETAIL: Array value must start with “{” or dimension information.
: INSERT INTO “courses” (“created_at”, “name”, “updated_at”, “user_id”)
VALUES ($1, $2, $3, $4) RETURNING “id”
(0.6ms) ROLLBACK
Completed 500 Internal Server Error in 14ms
…
PG::InvalidTextRepresentation - ERROR: malformed array literal: “2”
DETAIL: Array value must start with “{” or dimension information.
:
…