Rails 4 and PostgreSQL Arrays

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.
:

those errors are getting when I pass values from form, but I’ve try in
rails console works fine,now I think problem with

<%= hidden_field_tag( “course[user_id][]”, @course.user_id) %>

I hope anyone solve this problem to me…!

irb(main):001:0> c = Course.new user_id: [1,2], name: “course1”
=> #<Course id: nil, user_id: [1, 2], name: “course1”, 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>
irb(main):002:0> c.save
(0.2ms) BEGIN
SQL (0.7ms) INSERT INTO “courses” (“created_at”, “name”,
“updated_at”, “user_id”) VALUES ($1, $2, $3, $4) RETURNING “id”
[[“created_at”, “2015-02-24 09:41:57.932835”], [“name”, “course1”],
[“updated_at”, “2015-02-24 09:41:57.932835”], [“user_id”, “{1,2}”]]
(0.6ms) COMMIT
=> true
irb(main):003:0> c.valid?
=> true

On 24 February 2015 at 12:19, Sai Ch [email protected] wrote:

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

Why have you got user_id as an array?

Colin

In golf course game, some user create some course, after then he may
want to give course credentials to other users. for that purpose i will
take user_ids in array format.

For example bitbucket. we give our repository credentials to other users
who are registered in bitbucket,for edit,modify etc… that repo.

like that user need to perform CRUD operations on course actually
created by other user.

Please quote the previous message so that it is easier to follow the
thread. Anyone reading your message will have no idea what you are
talking about.

On 24 February 2015 at 12:55, Sai Ch [email protected] wrote:

In golf course game, some user create some course, after then he may
want to give course credentials to other users. for that purpose i will
take user_ids in array format.

You have course belongs_to user, that means it belongs to only one
user. You cannot have an array of users that it belongs to. If you
want a course to be associated with multiple users you need
has_and_belongs_to or has_many :through. Have a look at the Rails
Guide on ActiveRecord Associations for more information.

If you have not already done so then I advise working right through a
good tutorial such as railstutorial.org (which is free to use online).
That will show you the basics of rails.

Colin