How to make an ActiveRecord::Base object to have a list of strings?

Hi,

I’m new to Rails and already did some stuff with ActiveRecord
associations but I cannot do one thing:

I’ve got a

class Paragraph < ActiveRecord::Base
end

And I want each Paragraph to contain a list of words (each word is
simply a String). I want to have a table with all Paragraphs and save/
load them. And finally get a json rep of a paragraph. Simple. If I do

class Paragraph < ActiveRecord::Base
has_many :words
end

class Word < ActiveRecord::Base
belongs_to :paragraph
end

and put corresponding stuff in the db schema

create_table “words”, :force => true do |t|
t.integer “paragraph_id”
t.string “content”
end

So, this would work, and a json rep of a paragraph would look like
(stripped some stuff for clarity)

{“words”: [{“content” : “Once”}, {“content” : “upon”}, {“content” :
“a”}, {“content” : “time”}]}

but I don’t want this extra complexity so I want to get something like

{“words”: [“Once”, “upon”, “a”, “time”]}

A plain array of strings without the extra nesting.

But I can’t do

class Paragraph < ActiveRecord::Base
has_many :words, :class_name => “String”
end

without the Word class at all

===

I could add some additional to_json parameters I guess and tweak it to
give me what I want but it’d be ugly.
How do I achieve this with ActiveRecord? I guess a paragraphs_words
join table would work but I can’t work out the details.

Thanks,
Martin

This won’t answer your question but I hope it helps you because your
approach sounds too complicated to me, unless it is just for playing
and learning. Why would you want to store each word in a record in a
table? What if a word belongs to more than one paragraph?

How about hiding the “ugly” code on a to_simplified_json method in your
Paragraph model?

On Tue, Oct 19, 2010 at 12:46 AM, Gogov [email protected] wrote:

end
(stripped some stuff for clarity)
A plain array of strings without the extra nesting.
I could add some additional to_json parameters I guess and tweak it to
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to

[email protected][email protected]

.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Erol M. Fornoles
http://erolfornoles.posterous.com

http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

@pepe:
Yeah, one word could belong to many paragraphs but it was actually
more for experimentation purposes.
With Hibernate in Java one could use annotations like @Embedded to
achieve something similar so I wanted to compare Hibernate and
ActiveRecord but in reality, you’re right, it is a bit too complicated
(:

@Erol
Yep, I guess I’ll end up doing just that (: Just wondered if there is
something neater to do.

Thank you,
Martin

On Oct 18, 5:46pm, Gogov [email protected] wrote:

===
class Paragraph < ActiveRecord::Base
has_many :words, :class_name => “String”
end

is

serialize :words, Array

what you are looking for ?

Fred

I guess I can use that. Thank you!

Martin

On Oct 19, 1:07pm, Frederick C. [email protected]

Serialize ? What do that ?

Matias F. wrote in post #956786:

Serialize ? What do that ?

It does exactly what the docs say it does.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]