Re: Further to postgres on Rails in Fedora 18 but OT

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, May 16, 2013 at 19:57:46 +1000,
  Roger <arelem@xxxxxxxxxxx> wrote:

In sqlite, the string tables contain numbers so the rails to_i changes them to integer for adding.
How can I do this with postgres please?

You can cast text to integer using ::integer. For example:
bruno=> select '123'::text::integer;
int4 ------
  123
(1 row)

Thanks in advance
Roger

The reason I didn't make the numeric fields integer, is because Rails likes to have a select system for fields inputting integers and I do not want this.

If you do this, I recommend putting a constraint on the fields so that they can only be integers. You can use regular expressions to do the matching.

I have been searching on google all day for answers but as usual, responses from Rails officianados is curt and lacks explanation.

For postgres questions I would start by looking at the postgres documentation. It is very good.

In this case it might not be obvious where to look, but what you want is covered under type casts.
http://www.postgresql.org/docs/9.2/static/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS

Note in my example above I used the :: notation because it is a lot more convenient to type than using the SQL standard CAST function. But you don't want to use that in real code as it will make it harder to switch databases later.

If you want to using formatting when converting between strings and numbers (mostly going from numeric types to string types) you can look at:
http://www.postgresql.org/docs/9.2/static/functions-formatting.html
--
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org




[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux