Learning Ruby to use serverside to manage an existing database - sql

I am very much a beginner to rails and I have a specific need for it. I wish to connect it to a flex app, and also use it to call a database.
For example when the following web page is visited:
?User=bob&id=4&lic=234
Take 'bob' and 4 and add them to an existing database table if the license number is valid and not already used. I don't want to use the standard database stuff, I will need to write my own SQL queries as well as have logic for checking other stuff to do with registering users etc.
It is already done with Coldfusion but I am having issues with it, and would like to have it work with Ruby instead.
But to start with, I just want the ability to call a web page (on localhost:3000) get the string after the URL "?User=bob&id=4&lic=234" in this case and output "User:bob id:4" to the screen. I want to learn what the code is doing rather than have the least lines of code solution. I would rather use as little of the rails framework to do this as this will help me learn the language. I won't be making the standard website so learning the rails framework won't be so useful to me.
Thanks for the comments, sinatra seems to be what I'm looking for, however I am having problems connecting with databases, mysql in this case:
I'm following this:
http://community.active.com/blogs/productdev/2011/02/28/using-activerecord-3-without-rails
Trying to use a Mysql database in this instance.
However I get this error:
LoadError: Please install the mysql adapter: gem install activerecord-mysql-adapter (193: %1 is not a valid Win32 application. - F:/Software/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/mysql-2.8.1-x86-mingw32/lib/1.9/mysql_api.so)
So I tried:
gem install activerecord-mysql-adapter
However:
gem could not be found
So I did:
install instead activerecord-jdbcmysql-adapter-1.2.2
However it still didn't work.
When this is working, how do you make arbitrary SQL calls?

Have a look at Thin or Sinatra and combine it with Active record. Using Active Record to handle your database and models is far superior to doing it all by yourself.
If you insist on doing it by yourself. You can use the mysql2 gem or postgresql gem directly

Related

Can you recommend a data grid class/gem for Ruby on Rails that supports dynamic column selection?

I'm looking for a rails gem (or possibly several together) that will be the basis of the user facing front end for my application.
I'm constraint by a few things -
First, my user base is very technically challenged. All of the UI pieces have to be very easy to understand (in other words they've been seen a lot). It will be a stretch for these users to click on a column header and expect it to sort without some kind of prompting.
Second, the application flow needs to be very simple. As I mentioned in the first condition if I spread this out into a lot of small actions I'm likely to loose my user.
The core of the problem is that I have a dataset with 15 columns. I'd like to have the ability to:
have the users dynamically select which columns to view at one time
sort on any column in the view
filter the results (via text and attribute search)
pagination
I don't need any editing capabilities.
I've googled around for "ruby on rails datagrid" without much luck. I'm developing on Rails 3.1. Thank you for any help!
Check out the rails-casts on doing this with using the will_paginate gem and some sorting code: http://railscasts.com/episodes/240-search-sort-paginate-with-ajax and http://railscasts.com/episodes/228-sortable-table-columns
I'd take a look at DataTables. It's a JS table and there are some ruby gems that wrap it, e.g.: jquery-datatables-rails. There is also a railscast about it.
Try Datagrid - ruby library that helps you to build and represent table-like data with:
Customizable filtering
Columns
Sort order
Localization
Export to CSV

How do I access SQL from XPages

What is the process to access data from a SQL data source and have it fill in a list box control so that the user may select one of the values?
I have been given the name of the database and server, the login ID and password.
Code samples would really be appreciated as I have never done any SQL coding.
The latest Extension Library on OpenNTF ( extlib.openntf.org ) has a whole bunch of Relational Database extensions.
You'll need to get the JDBC drivers for whatever SQL server your going to be accessing and then take a look at the ExtLib demo application on how to create the JDBC connector from your application. Once the connector is in place you can then just the new controls in ExtLib to easily create a view pane etc.
You will also need more then the SQL server, username and password, you'll need to find out the different tables that you'll be accessing so that you can reference them from your Xpages application.
I've created a video showing JDBC access from XPages: http://www.youtube.com/watch?v=p6oRCsTsVqc
Wait for the book that will e released soon about the extlib. I know Jeremy hodge wrote the chapter so you might be able to get some info from him.
From an answer I gave earlier: you might want to check out the blog post announcing the JDBC support . It has an excellent video explanation and a link to a slide deck.
Also, take a look at Xpages101 lesson 61. It's paid-for content, but well worth it if you're serious about Xpages development.
If you want to combine Upgrade Pack 1 (UP1) with the Extension Library JDBC parts, then make sure to use the Extension Library that matches exactly the UP1 version. This is version 853-20111215 of the Extension Library. Then you can use the update site method to only deploy the experimental parts of the Extension Library (com.ibm.xsp.extlibx.feature_8.5.3.20111215-0914.jar).
For newer releases of Extension Library things might (will) have changed so that UP1 and Extension Library can not work together.
When UP2 is released, you need to remove the Extension Library package and deploy UP2. At that point in time UP2 might contain the JDBC support.
Roy,
As the previous posters put the ext library stuff will make it a little more "Drag and Drop", but you can use regular JDBC connection to get the data you want, Its pretty simple, but a lot more code than using Domino as a backend. You might want to look at this John Mackey blog post about doing a very similar thing...http://www.jmackey.net/groupwareinc/johnblog/johnblog.nsf/d6plinks/GROC-7G9GT4
Keep in mind that you need the actual ext. library for this. The upgrade pack does not contain the JDBC stuff.
Edit:
Keep in mind that if you don't need "LIVE" data access, and the information you want is fairly static you could always just use a lotusscript agent to pull the data down into Notes Documents. Run that once a day or whatever. No fancy XPages stuff needed. That's fairly common coding and practices with examples available.
Then just have the list box pull from the documents you brought down.

Rails: Best practice for handling development data

I have the following scenario:
I'm starting development of a long project (around 6 months) and I need to have some information on the database in order to test my features. The problem is that right now, I don't have the forms to insert this information (I will in the future) but I need the information loaded on the DB, what's the best way to handle this? Specially considering that once the app is complete, I won't need this process anymore.
As an example, lets say I have tasks that need to be categorized. I've begun working on the tasks, but I need to have some categories loaded on my db already.
I'm working with Rails 3.1 btw.
Thanks in advance!
Edit
About seeds:I've been told that seeds are not the way to go if your data may vary a bit, since you'd have to delete all information and reinsert it again. Say.. I want to change or add categories, then I'd have to edit the seeds.rb file, do my modifications and then delete and reload all data...., is there another way? Or are seeds the defenitely best way to solve this problem?
So it sounds like you'll possibly be adding, changing, or deleting data along the way that will be intermingled amongst other data. So seeds.rb is out. What you need to use are migrations. That way you can search for and identify the data you want to change through a sequential process, which migrations are exactly designed for. Otherwise I think your best bet is to change the data manually through the rails console.
EDIT: A good example would be as follows.
You're using Capistrano to handle your deployment. You want to add a new Category, Toys, to your system. In a migration file then you would add Category.create(:name => "Toys") or something similar in your migration function (I forget what they call it now in Rails 3.1, I know there's only a single method though), run rake db:migrate locally, test your changes, commit them, then if it's acceptable deploy it using cap:deploy and that will run the new migration against your production database, insert the new category, and make it available for use in the deployed application.
That example aside, it really depends on your workflow. If you think that adding new data via migrations won't hose your application, then go for it. I will say that DHH (David Heinemeier Hansson) is not a fan of it, as he uses it strictly for changing the structure of the database over time. If you didn't know DHH is the creator of Rails.
EDIT 2:
A thought I just had, which would let you skip the notion of using migrations if you weren't comfortable with it. You could 100% rely on your db/seeds.rb file. When you think of "seeds.rb" you think of creating information, but this doesn't necessarily have to be the case. Rather than just blindly creating data, you can check to see if the pertinent data already exists, and if it does then modify and save it, but if it doesn't exist then just create a new record plain and simple.
db/seeds.rb
toys = Category.find_by_name("Toys")
if toys then
toys.name = "More Toys"
toys.save
else
Category.create(:name => "More Toys")
end
Run rake db:seeds and that code will run. You just need to consistently update the seeds.rb file every time you change your data, so that 1) it's searching for the right data value and 2) it's updating the correct attributes.
In the end there's no right or wrong way to do this, it's just whatever works for you and your workflow.
The place to load development data is db/seeds.rb. Since you can write arbitrary Ruby code there, you can even load your dev data from external files, for instance.
there is a file called db/seeds.rb
you can instantiate records using it
user1=User.create(:email=>"user#test.com",
:first_name=>"user",
:last_name=>"name",
:bio=>"User bio...",
:website=>"http://www.website.com",
:occupation=>"WebDeveloper",
:password=>"changeme",
:password_confirmation=>"changeme",
:avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
)
user2=User.create(:email=>"user2#test.com",
:first_name=>"user2",
:last_name=>"name2",
:bio=>"User2 bio...",
:website=>"http://www.website.com",
:occupation=>"WebDeveloper",
:password=>"changeme",
:password_confirmation=>"changeme",
:avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
)
Just run rake db:seed from command line to get it into the db

rails 3.0.3 activerecord wants to pluralize certain mysql tables all of a sudden

This is in development mode. ActiveRecord::StatementInvalid. This is an example: I have a model called DisplayAds. Now activerecord wants to query the table as display_adses instead of display_ads. I have another model called Media. Activerecord use to query the table medias. Now it wants to query a table called medium.
I know that DisplayAds is not the proper naming convention but it was working until now.
I realize I can fix this by putting set_table_name in the model.
I have not upgraded rails and everything has been working fine for months.
I have a live production version of this site and it is not experiencing this problem.
I do git status and I do not see any config files as being modified.
What could I have done to start this happening? How can I stop it?
Have you recently upgraded from 2.3.5/8 to 3.0.x? I had the same problem (same thing with medias).
You'll want to add irregular Inflections in your config/initializers/inflections.rb file
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'media', 'medias'
inflect.irregular 'display_ad', 'display_ads'
...
end

Drop and Recreate a single table (on Heroku)

My app is in beta, and I've been doing limited testing of a feature that involves a new model. After a fair amount of testing I had to make a structural change that makes the old data non-functional.
What I need to do is just drop and recreate one table. I know that I could do this in a migration, but that seems like such a hack. In a local dev copy I would just use db:reset, but in the beta app I don't want to lose data in any tables except this one.
Is this a simple way to instruct a production app to drop and recreate a single table. In my case, I'm deploying with Heroku, in case that affects how you would solve this issue.
To empty a table on Heroku without changing the schema, in your application's directory:
$ heroku run console
Ruby console for myap.heroku.com
>> ModelName.delete_all
>> exit
I know that I could do this in a migration, but that seems like such a hack.
It's not a hack. It's precisely what migrations are designed to do.
You need to rerun the migration for that table to make structural changes. I haven't used ActiveRecord before, but I'd also delete the data in the table using ModelName.delete_all from heroku console.
heroku run console
irb(main):001:0> ModelName.delete_all
And you are done.