0

I want to insert hashes like below into a mysql column,

{:sub => ["at","div.product-info"],
     1 => [["at","span#hs18Price"]],
     :avail => ["at","strong.out_stock"],
     :soffer =>  ["at","div.freebie"], 
     :stime =>["search","div[@class='costs']//span[@class='days']"],
     :scost => [300,30] }

I tried, its throwing the following error,

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'costs...

Any help?

EDIT:

Forget active record and serialization. I want to do it in a raw sql.

4 Answers 4

2

You'll need to escape the single quotes in your hash

Sign up to request clarification or add additional context in comments.

Comments

2

If you want to store Hash inside column in MySQL (or any other database supported by AR*) you have to:

1) define column as text

add_column :users, :the_whatever, :text

2) define serialization of column so that ActiveRecord knows what to do with you data. You do this by setting serialize option to model.

serialize :the_whatever, Hash

* Some databases (like PostgreSQL) also provide some other types more suited for storing Hashes. See Railscasts Hstore.

Comments

1

If you're willing to switch the database: PostreSQL has hstore for this.

1 Comment

Postgres 9.2 has a JSON data type as well.
0

Your sql column must be of type text. You might need to serialize your hash first too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.