Comparing string before and after DB save in Rails? - ruby-on-rails

I am trying to validate uniqueness on an article description. After I save it to the DB, if I retrieve it it comes back in a stripped down format (missing some chars). If I put a validator on the uniquness of the text in the desc. it doens't get called. If I do a find_by_desc it fails since the text is slightly different. Any ideas? I know the table is UTF-8, but really most of the characters are a few line endings and some dashes (-) here or there.

why you don't create an hash-code for that? create it before save, in validation check for the hash comparing. I don't suggest you to compare TEXT fields

Related

Appsheet - special character inside columns' titles but not true

I'm receiving the following message:
Table name 'IINR(finishes)' uses special characters that are used in expressions and formulas. Consider renaming the table to remove these special characters.
But I checked and there are no special characters inside any title.
I also tried to create a new app using a different GSheets, in which a copy paste the titles of the columns and in this case, no warning message.
What can it be the reason?
Thanks!
In the end (I didn't read well the message) the problem was on the title of the spreadsheet (table) and not at column level..

Have anyone had is kind of error before and how to fix it?

This happened when updating text area.
If you have a valid EF model with the correct field length, this shouldn't be a problem (for example, by decorating with Length attributes)
This type of error generally occurs when you try to put characters or values more than what you have specified in your table schema.
Like in that case: you specify varchar(10), but you are trying to store a value that contains 19 characters.
Solution:
Check your parameter sizes against column sizes and the field(s)
Try updating each property one by one with a .SaveChanges() to find out which one is causing the error.

Handling commas in data with VarArrayOf

I am using Locate with multiple fields and sometimes I am getting an "Incorrect number of search fields" error and have tracked it down to the data.
tbl.Locate('LName;FName;Stuff',
VarArrayOf([LName+','+FName','+Stuff]),[loCaseInsensitive]);
Sometimes it worked and sometimes it did not and I traced it down to a Comma in the Stuff if it included a Comma in the text.
Stuff = "My dog is gray" would work.
Stuff = "I am at work, but not happy" would not. That extra Comma made it look like four data fields.
Do I have to Parse every string of incoming data for Commas and if any found replace them with what for the Locate to work? The included Comma version will have been stored without Parsing or modifying as I have no idea what they may have entered. "I am at work, but not happy" is a valid input by the user.
Thanks.
Do not concatenate all the search values into a single string. Instead, place them as distinct items in the array, just like the example in the documentation. That is, don't include the commas in the string:
tbl.Locate('LName;FName;Stuff',
VarArrayOf([LName, FName, Stuff]), [loCaseInsensitive]);

Rails wont return decimals correctly

I'm using Rails 3.2.5 and when it return a value o :price for editing it just brings one decimal, eg. 600.0, i need that i bring 2 decimals (eg. 600.00) in database is recorded 600.00, in my locales i already set for 2 decimals, and still dont work.
I tried
number_to_currency(:price, :precision => 2)
but it works well for a view like "show", i need that it return the propper value for editing, on textfield.
in my migration the field "price" is set do decimal(15,2).
Can someone help?
Thank You!
The text fields are operating on the "native" value of the field, as it is handled by ActiveRecord, not as it is stored in the database.
Currency fields are tricky since ActiveRecord is translating between the database representation and the ruby/rails representation, a BigDecimal.
If your goal is that you want to show your users $1,000.00 and enable them to edit it, some ideas:
Use the Money gem Ylan S refers to.
Use an Edit in place widget Eg screencast. You'd use number_to_currency to display the value. When clicked, the input field would show the value without the currency symbol, commas for thousands separators, etc. Note that this is how Excel works: when you edit the value of a currency field, you don't enter 1,000. You enter 1000.
In my experience, instead of using decimal for storing currency, it's best to store the amounts in cents, as an integer. This will take care of multiple problems, including the one you are having now.
I have had much success in the past using the Money gem and its companion money-rails.
Issue solved using 'delocalize' gem.
Old but gold! :) Thank you all!
The simplest way to have your text_field display a formatted value is to pass it explicitly, like this:
f.text_field :price, :value => number_to_currency(:price, :precision => 2)
You'll need to interpret (and possibly re-format) the value in the controller method that handles the form.
See How can I format the value shown in a Rails edit field?

How can I get Rails to interpret a text field as a datetime

My database has a datetime field, and I want to be able to create new entries. Obviously the Rails datetime_select helper isn't the most user friendly thing to have in your form.
I'd rather have a text field for the datetime (or one for the date, and one for the time) and interpret the inputs like PHP strtotime can.
I might just be searching the wrong keywords. Surely this has been discussed in great depth somewhere.
Thanks
:0)
Check out Railscast #32, I've used this method a few times and it works pretty well.

Resources