# Rails 4 and PostgreSQL Arrays

**URL:** <https://discuss.rubyonrails.org/t/rails-4-and-postgresql-arrays/70317>\
**Category:** rubyonrails-talk\
**Created:** [February 24, 2015, 12:19pm UTC](https://discuss.rubyonrails.org/t/rails-4-and-postgresql-arrays/70317 "2015-02-24T12:19:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 24, 2015, 12:19pm UTC](https://discuss.rubyonrails.org/t/rails-4-and-postgresql-arrays/70317/1 "2015-02-24T12:19:51Z")

</div>

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 &nbsp;&nbsp;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"} &nbsp;&nbsp;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\> &nbsp;&nbsp;&nbsp;(0.4ms) BEGIN &nbsp;&nbsp;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" &nbsp;&nbsp;&nbsp;(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. : ...............................................

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 24, 2015, 12:24pm UTC](https://discuss.rubyonrails.org/t/rails-4-and-postgresql-arrays/70317/2 "2015-02-24T12:24:26Z")

</div>

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 &nbsp;&nbsp;&nbsp;(0.2ms) BEGIN &nbsp;&nbsp;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}"]] &nbsp;&nbsp;&nbsp;(0.6ms) COMMIT =\> true irb(main):003:0\> c.valid? =\> true

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 24, 2015, 12:27pm UTC](https://discuss.rubyonrails.org/t/rails-4-and-postgresql-arrays/70317/3 "2015-02-24T12:27:14Z")

</div>

> 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

Why have you got user\_id as an array?

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 24, 2015, 12:55pm UTC](https://discuss.rubyonrails.org/t/rails-4-and-postgresql-arrays/70317/4 "2015-02-24T12:55:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 24, 2015, 1:44pm UTC](https://discuss.rubyonrails.org/t/rails-4-and-postgresql-arrays/70317/5 "2015-02-24T13:44:35Z")

</div>

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.

> 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
