Ruby-based attributes, and input splitting

I’m sorry if these are very simple questions, I just haven’t been able
to find useful answers for my problems.

I’ve been programming in Ruby for a couple years, and recently started
with Rails (having felt most of Ruby seems to be in that direction,
these days), so I have a reasonable amount of knowledge in ruby
scripting but know very little about how to get Rails to do my bidding.

So far I’ve followed a couple “create a blog” tutorials, and have gained
a few steps in the right direction, but I still have a few issues.

The server that hosts my applications only runs Rails version 2.3.14, so
please try to keep any code/suggestions compatible.

What I would like, in my project, is for there to be two input fields:
“subject” and “keywords”. One subject can have multiple (limited; maybe
up to 7? doesn’t really matter) keywords, which will be found in the
“keywords” field and delimited by commas.

Obviously in ruby it would be a simple keywords.split(‘,’).

Each keyword (after being split) will also have a few attributes which
are entirely ruby-defined. Suppose they’re length and number of vowels.
Basically things that I want stored in the database which aren’t
strictly input by a user.

As far as I know, the first steps in Rails to set up my database would
be to use the following commands:

ruby script/generate scaffold search subject:string words:text

ruby script/generate scaffold keyword word:string length:integer
vowels:integer search:references

(and then I would add the “has_many” to search and “belongs_to” to
keyword).

And basically all the app needs to do, after, is display all of the
previous subject words, and for each post each keyword and its
attributes (length, etc.).

But every tutorial I can find tends to deal with inserting user input to
the database, rather than inserting the output of Ruby scripts.

That looks like it should set things up for me, but the real questions
I’m facing are:

  1. How do I add a variable number of things (keywords) to the database
    from within Rails?
  2. How do I store entirely-ruby-determined attributes like a string’s
    length?

My current ideas:

The Active Record section in the Rails API
(ActiveRecord::Base) gives me a
start.
It looks like I might be able to perform the split in the “words” model
and use a small loop that has something like the following in it:
new_word = Keyword.new(word=in_words[i], length=in_words[i].length,
vowels=in_words[i].vowels)

Would that be appropriate? How would I (or even, should I for something
small like this without much searching, just parsing and displaying) set
the reference foreign key to the current “Search”?

Sorry for such a long post about a simple question, and thank you for
having the patience to read through it!
-Dylan

On 11 January 2012 23:44, Dylan R. [email protected] wrote:

Each keyword (after being split) will also have a few attributes which
vowels:integer search:references

That looks like it should set things up for me, but the real questions
I’m facing are:

  1. How do I add a variable number of things (keywords) to the database
    from within Rails?

In the controller parse the string and loop round, creating the new
objects and saving them one at a time.

  1. How do I store entirely-ruby-determined attributes like a string’s
    length?

Don’t store values that can be re-calculated, such as the string’s
length in the database unless efficiency becomes an issue. Add member
methods to the model to return the calculated values then in the
future if you really need to then you can add the attributes to the
database. Almost always the bottlenecks in an app will not be in the
places you initially imagine them to be. If you did want to store
these values though then you could use a before_filter to calculate
them when the record is created or updated.

Colin

vowels=in_words[i].vowels)
Posted via http://www.ruby-forum.com/.


You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw

Colin L. wrote in post #1040530:

In the controller parse the string and loop round, creating the new
objects and saving them one at a time.

That makes a ton of sense. Thank you very much!

  1. How do I store entirely-ruby-determined attributes like a string’s
    length?

Don’t store values that can be re-calculated, such as the string’s
length in the database unless efficiency becomes an issue. Add member
methods to the model to return the calculated values then in the
future if you really need to then you can add the attributes to the
database. Almost always the bottlenecks in an app will not be in the
places you initially imagine them to be. If you did want to store
these values though then you could use a before_filter to calculate
them when the record is created or updated.

I sort of simplified the calculations I want to perform on the strings
to get a general idea of this sort of thing. In actuality it includes
web-access/data-mining, so would be a very bad idea to perform it
every single time (for one, it would become hugely slow if I keep old
results, but more importantly I certainly don’t want to be excessive
with anybody else’s bandwidth; it would be a very rude thing of me to
do!)

before_filter could work as a solution, I’ll try it out. There won’t be
any edit option, or anything like that. Either that or I was thinking
that it might simplify things to scratch the keywords section in the
database, and serialize it as an array (or 2-d array), only because
currently the ideas for the app don’t take advantage of any
database-enhanced features, such as search; it would simply pour out the
past 100 entries sequentially. Of course that might harm any future
expandability/new features I might want to add. I might be better off
building a good foundation and using the looped control idea.

Anyway, thank you kindly for the ideas. I really appreciate them!
~Dylan