Integer array columns

Postgres database.
I have a categories table, with two columns id, and name.
I have a recipes table, with a category_ids column.

I want each recipe to belong to one or more category.

in recipe_controller.rb I have this:

def create
@recipe = Recipe.new( @params[‘recipe’] )
@recipe.category_ids = @params[‘recipe’][‘category_ids’].map{
|s| s.to_i }
@recipe.save
end

But I keep getting an error during the INSERT, saying column
category_ids is of type ‘integer[]’ but expression is of type ‘integer’.
In the generate SQL, the value for category_ids is always 1, no matter
what values I have placed in the array.

How can I get my integer array stored?

Thanks,

~S

Shea M. wrote:

@recipe.category_ids = @params['recipe']['category_ids'].map{

Thanks,

~S

Maybe I need to simplify the problem/question. I have a table with a
column which holds an array of integers. Rails treats the column as an
integer column, which fails when trying to insert data, as the arrays I
assign to the record object seem to have .to_i called on the array as a
whole, resulting in a 1 or a 0.

How can I insert data in an array column? Is there a way to tell rails
that column x is of type integer[]? I know both mySQL and Postgres both
have array columns, so someone must have come across this before, yet I
can’t find anything on google.

~S

On Monday, June 19, 2006, at 9:47 AM, Shea M. wrote:

@recipe = Recipe.new( @params['recipe'] )

How can I get my integer array stored?
whole, resulting in a 1 or a 0.
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Near as I can tell, MySQL does not support integer arrays. If you
really want to save an array in a column, then you should probably
serialize it.

take a look for ‘serialize’ in the Rails API.

_Kevin

Shea M. wrote:

def create
@recipe = Recipe.new( @params[‘recipe’] )
@recipe.category_ids = @params[‘recipe’][‘category_ids’].map{
|s| s.to_i }
@recipe.save
end

Possibly OT, but is there a reason you aren’t using

has_and_belongs_to_many :categories ?

Alan

Alan F. wrote:

has_and_belongs_to_many :categories ?

Alan

Yes, ignorance.

In recipe, I had
belongs_to :category

and in category, I had
has_many :recipes

I am at work now, tonight I will give your suggestion a try:
So I should try changing the recipe model to has_and_belongs_to_many,
and the insertion should work?

Thanks,

~S

Near as I can tell, MySQL does not support integer arrays. If you
really want to save an array in a column, then you should probably
serialize it.

take a look for ‘serialize’ in the Rails API.

_Kevin
I can’t remember where I read that it does, but I did turn up this,
http://jeremy.zawodny.com/blog/archives/000841.html
Though I can’t seem to turn up anything in the official release notes.
So now I am unsure.

I will look at serialize tonight when I get home.

Thanks,

~S

Alan F. wrote:

has_and_belongs_to_many :categories ?

Alan

Ok, I just finished implementing this. It is exactly what I needed.
Thanks.

But for my when creating a new recipe, I want the possible categories to
be in a multi-select box.

<%= options_from_collection_for_select( @categories, ‘id’, ‘name’,
@recipe.categories.map{|t| t.id}) %>

My trouble was the right categories selected, when editing a recipe. Is
this map{} trick the right way to do it? (It works, btw).

Thanks,

~S

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs