I know that I'm missing something simple here, but the solution is
evading me.
I want to get the values in the fruits field for my first order.
In script console I type this:
Cart.find(:first, :select => "fruits")
=>#(Car fruits: "apple, orange, pear")
c = Cart.find(:first)
c.fruits += ", grapefruit"
c.save
Now, that all said, managing a list of line items on a order using a
string can be problematic.
You may want to have your Cart have_many LineItems and each line item is a
single product, quantity, price... that preferably references a Product
model.
Since that's "so basic", perhaps the read-only part that :select imposed was the problem. If fruits is something else, provide a bit more detail and I could be a bit more helpful.
c = Cart.find(:first)
c.fruits += ", grapefruit"
c.save
Perfect!
Now, that all said, managing a list of line items on a order using a
string can be problematic.
Yes, it is problematic, but it's how the application has been set up and
I'm not able to change it at this time. Legacy data is fun!
Let me ask one other question, I use that same string to query back a
results list. Now I have "apple, orange, pear, grapefruit".
Let's say I want to query that same table and find all of the other
orders that contain any one of these items. Given what I have, is my
only option to write a gsub method that's ugly and create a custom
finder for it?
Since that's "so basic", perhaps the read-only part that :select
imposed was the problem. If fruits is something else, provide a bit
more detail and I could be a bit more helpful.
I did have an initial problem with a non-savable record. I had to pull
the entire record and remove the :select.
My brain is trying to put many things together and things are getting
very lost in the process.
c = Cart.find(:first)
c.fruits += ", grapefruit"
c.save
Perfect!
Now, that all said, managing a list of line items on a order using a
string can be problematic.
Yes, it is problematic, but it's how the application has been set up and
I'm not able to change it at this time. Legacy data is fun!
For various definitions of "fun"
Let me ask one other question, I use that same string to query back a
results list. Now I have "apple, orange, pear, grapefruit".
Let's say I want to query that same table and find all of the other
orders that contain any one of these items. Given what I have, is my
only option to write a gsub method that's ugly and create a custom
finder for it?
search = "grape"
Cart.find(:all, :conditions => ['fruits like ?', "%#{search}%"])
will return all Cart's whose fruits have "grape" in them somewhere.
Post a real string to be sure, but I suspect that the /, / (comma space) isn't matching between the other fruits. Try /,\s*/ to get (comma followed by optional whitespace)