Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
prom candy
Dec 16, 2005

Only I may dance
Your association method can't be named something that's already a column in your database. If you want to have something that belongs_to an owner your column should be owner_id. You're basically getting a method collision right now because with active record any field in your database (with a few exceptions) becomes a method on your model, and when you declare an association that also creates methods on your model. So when you type Thingy.owner it doesn't know if you're referring to the association or the value in the DB field.

Your issue is completely unrelated to the inheritance.

Adbot
ADBOT LOVES YOU

Physical
Sep 26, 2007

by T. Finninho

prom candy posted:

Your association method can't be named something that's already a column in your database. If you want to have something that belongs_to an owner your column should be owner_id. You're basically getting a method collision right now because with active record any field in your database (with a few exceptions) becomes a method on your model, and when you declare an association that also creates methods on your model. So when you type Thingy.owner it doesn't know if you're referring to the association or the value in the DB field.

Your issue is completely unrelated to the inheritance.
Now I can't do in the view

@it_device.owner.name

How do I get that relationship working again? here are my updated has/belongs

user.rb
has_many :it_devices, :foreign_key => "owner_id"

itdevices.rb
belongs_to :owner, :class_name => "User", :foreign_key => "owner_id"

edit: OH SWEET! Got it hooked up! I had to go back and change my select tag and add :
:name => "it_device[owner_id]"

This took about 2 hours or 3. Oh well I feel like a champion now! This is my 1st week of real RoR programming. Thanks prom candy, you were the only place on the internet that pointed out the final piece I needed.

Physical fucked around with this message at 22:11 on Mar 1, 2012

Look Around You
Jan 19, 2009

syntaxrigger posted:

To sort of piggy back off of Fat Whale's question, if I really wanted to learn a scripting language as well as web development, which is a better suggestion: Ruby or python? I am coming from a java background.

I keep going back and forth on this issue and I really have no idea how to solve the problem. My working solution right now is to learn both, starting with Ruby, and then make a decision.

My primary concerns are a path that will develop me into a better programmer and to be as marketable as possible.

I don't know much about marketing yourself, but in terms of being a better programmer, it probably won't matter all that much in the end. New languages aren't hard to pick up as long as they're within roughly the same paradigm.

It should be pretty trivial to pick up Ruby and Python coming from Java (all three are Object Oriented); you'll just have to learn some of the differences, mainly dynamic typing.

To compare Ruby and Python though: Ruby has more of an emphasis on blocks and closures, and is better for implementing domain specific languages. It also allows for multiple ways to do something and tends to not emphasize one way, a philosophy coming from Perl. Python on the other hand places more emphasis on standard forms, where if you have a problem usually there is really only one way that you "should" solve it. A term you'll hear a lot is "Pythonic", which boils down to following the philosophy of the language and being the generally accepted way of doing things.

That being said, if you want to really make yourself a "better" programmer, I'd recommend learning a functional language like Haskell or Lisp. They're really awesome languages in general, and functional programming is a good way to get a better grasp of recursion (especially considering that you don't really have loops at all!). It's also just good to step out of your comfort zone sometimes. A good book for learning Haskell is Learn You a Haskell for Great Good by fellow goon Bonus. Lisp has a couple good books too, notably SICP and How to Design Programs, which is sort of SICP lite.

Schatten
Jul 7, 2002

Das ist nicht meine
schnellen Rennwagen
I'm still quite new at rails, and have been tinkering with hobo 1.3. Wondering if a fellow goon could lend a hand.

The project: creating an order/fulfillment application with a trouble ticket system. We produce OEM systems, ship them out, and they have maintenance on them, various versions of software, etc. It's just about done. Relationships are there. Everything workrs fine, but I need a dynamic menu for the following:

Order a maintenance contract (maint.rb) -
On this page, when setting up a new maintenance contract - select the company (model), have that dynamically populate a list of order numbers (model), then list out the serial numbers tied to each of those orders to select which ones will be on the maint renewal.

And also tips on how to handle this on the edit page.

Tom has an example of this here: http://cookbook.hobocentral.net/recipes/15-dynamically-populated-select-menus
But I cannot get my mind around translating that to how I need it to be.

The other small 'would like to have' item I'd like is if someone pulls up a serial number for a model and you could tell if it is under maintenance. The maint model has start/end dates, but there could be four maint orders for that system, so you'd have to look for the latest, then compare dates and show 'in maint' or 'out of maint' for those.

If someone can lend a hand, give me hints or anything else, I'd be grateful or even buy you an avatar/plat. on here, or something of greater value depending on the time/help/efforts put forth.

syntaxrigger
Jul 7, 2011

Actually you owe me 6! But who's countin?

Look Around You posted:

I don't know much about marketing yourself, but in terms of being a better programmer, it probably won't matter all that much in the end. New languages aren't hard to pick up as long as they're within roughly the same paradigm.

It should be pretty trivial to pick up Ruby and Python coming from Java (all three are Object Oriented); you'll just have to learn some of the differences, mainly dynamic typing.

To compare Ruby and Python though: Ruby has more of an emphasis on blocks and closures, and is better for implementing domain specific languages. It also allows for multiple ways to do something and tends to not emphasize one way, a philosophy coming from Perl. Python on the other hand places more emphasis on standard forms, where if you have a problem usually there is really only one way that you "should" solve it. A term you'll hear a lot is "Pythonic", which boils down to following the philosophy of the language and being the generally accepted way of doing things.

That being said, if you want to really make yourself a "better" programmer, I'd recommend learning a functional language like Haskell or Lisp. They're really awesome languages in general, and functional programming is a good way to get a better grasp of recursion (especially considering that you don't really have loops at all!). It's also just good to step out of your comfort zone sometimes. A good book for learning Haskell is Learn You a Haskell for Great Good by fellow goon Bonus. Lisp has a couple good books too, notably SICP and How to Design Programs, which is sort of SICP lite.

I messed around with Lisp in college and have a passing knowledge of recursion. I guess my main focus is to learn web development as I feel it is a major part of programming that I am missing.

So the goal is to educate myself on web dev while learning a scripting language heavily tied to web dev.

I would say the big obstacle that I continually struggle with is the fear that I will "waste time" by "hitching my cart to the wrong horse". As I feel I have started my career in programming late, i.e. I am 29 and a year out of college and I only started programming when I got to college. Maybe this is probably an irrational fear, I dunno.

Feel like I need some sort of mentor, anyways rambling, thanks for the input

prom candy
Dec 16, 2005

Only I may dance
A big part of making yourself marketable in web dev these days is understanding front end development. Make sure you're learning javascript concurrently with whatever back-end language/framework you decide on. And I mean really learning javascript, not pasting jQuery plugin examples into your onclick attributes.

Johnny Cache Hit
Oct 17, 2011

syntaxrigger posted:

I messed around with Lisp in college and have a passing knowledge of recursion. I guess my main focus is to learn web development as I feel it is a major part of programming that I am missing.

So the goal is to educate myself on web dev while learning a scripting language heavily tied to web dev.

I would say the big obstacle that I continually struggle with is the fear that I will "waste time" by "hitching my cart to the wrong horse". As I feel I have started my career in programming late, i.e. I am 29 and a year out of college and I only started programming when I got to college. Maybe this is probably an irrational fear, I dunno.

Feel like I need some sort of mentor, anyways rambling, thanks for the input

Seconding the "don't worry about it" regarding Python v/Ruby. I started programming in PHP and Java, bounced to Python, and is now a Ruby dev. It's no big deal. From what I've seen good companies want good programmers, and mediocre companies only want {Java, Ruby, PHP, ...} programmers.

One thing you might want to look at is what's in demand in your area, especially if you're not in a major technology area like SF or NYC or whatever. Be sure to ask around, not just rely on job postings. I know some people have really negative opinions of recruiters, but I've found them invaluable in figuring out what skills you really need.

For example, almost all of my area's published job listings want ASP.NET programmers. But every recruiter I spoke with said that those jobs, and consequently those programmers, are dime a dozen in this area. But there are a ton of jobs in this area for Ruby developers that go unfilled because everyone's too busy with .NET.

At the end of the day, don't stress too much about what language to learn, because that's such a small part of programming. And IMO, I wouldn't look for jobs at companies that put too much focus on what languages you know.

syntaxrigger
Jul 7, 2011

Actually you owe me 6! But who's countin?

Kim Jong III posted:

Seconding the "don't worry about it" regarding Python v/Ruby. I started programming in PHP and Java, bounced to Python, and is now a Ruby dev. It's no big deal. From what I've seen good companies want good programmers, and mediocre companies only want {Java, Ruby, PHP, ...} programmers.

One thing you might want to look at is what's in demand in your area, especially if you're not in a major technology area like SF or NYC or whatever. Be sure to ask around, not just rely on job postings. I know some people have really negative opinions of recruiters, but I've found them invaluable in figuring out what skills you really need.

For example, almost all of my area's published job listings want ASP.NET programmers. But every recruiter I spoke with said that those jobs, and consequently those programmers, are dime a dozen in this area. But there are a ton of jobs in this area for Ruby developers that go unfilled because everyone's too busy with .NET.

At the end of the day, don't stress too much about what language to learn, because that's such a small part of programming. And IMO, I wouldn't look for jobs at companies that put too much focus on what languages you know.
Cool, thanks for this. I am in the dallas area, hoping to be able to move to Austin eventually. I think I might drop a line to the one recruiter I have on my linked in and see what he says about "valuable skills" in the austin area.

Sock on a Fish
Jul 17, 2004

What if that thing I said?
What's the best way to automate deployments and any migrations that might be required for those deployments?

Right now the deployment model I'm planning has hosts periodically running a configuration management tool (puppet or chef) which checks for new builds, grabs a new build if available, and runs that build.

I was going to have each host run db:migrate before they start running new code. I suspected that two hosts doing this at once would step on each other's toes, and when I tested it I confirmed my suspicion.

There are a few ways I could deal with this. I could implement some kind of distributed mutex, either in the database or in something like DynamoDB, and just have a new rake task that gets a lock and then invokes db:migrate.

I could designate one host as the migrate host. I'm planning on deploying with AWS auto-scaling groups, and that would make this method more complicated. For each deploy, I'd need to suspend terminate processes for the auto-scaling group, pick a migrate host, run migrations, and then resume terminate processes.

I could also spin up a single micro instance for each deploy that only does migrations and anything else that needs to be done once on a single host. Each deploy would cost an extra $0.02.

Or, I could just forget about automating migrations, and realize that any deploy that requires a migration is going to also require human intervention.

Am I missing anything? Or am I approaching this totally wrong?

Oh, I'm also investigating whether Heroku could work for us. It's my first week with this employer, and I don't know enough about the application stack to make that call yet.

prom candy
Dec 16, 2005

Only I may dance
Have you looked into Capistrano? Your set up is a little more complicated than mine but if there's someone out there who's solved your particular problem it's fairly likely that they've done it with Capistrano.

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!
One of the only complaints I have with Heroku is that it's exceedingly difficult to do actual seamless deployments.

On a normal setup, you need to push your code before you're able to do migrations, which means that your new code needs to be backwards compatible with an unmigrated database (possible, but much more difficult to do than the other way around, and can tend to lead to spaghetti code).

If you're paying for a dedicated DB, it should be possible to do your migrations prior to pushing up code, but that isn't a cheap option if you're just playing around or experimenting. (we already pay an OK amount to Heroku and haven't needed a dedicated DB set up yet.)

On top of that, even if you can do things perfectly seamlessly in regards to the database, when you do a push Heroku will switch everything over to the new slug before the app has had a chance to fully boot up. If you have a decent amount of gems or something slowing startup time this could be > 15 seconds before you're completely in the clear.

Currently the process I'm thinking of testing out that hopefully should make things completely seamless:

  • Create two heroku applications, live and deployment. Modify deployment to point to live's database.
  • Push all changes up to deployment, and migrate from there. (There is a seam here if the migrations themselves aren't compatible with live, but in most cases like adding columns this is usually fine)
  • Smoke test deployment, and make sure the dynos are up and running.
  • Switch over the domains (on heroku, not DNS) to the deployment application
  • Push to live, smoke test it, and switch the domains back when everything is looking good.

It's definitely a lot of steps, but I think everything could be automated by a script, and as far as I can tell it would be truly seamless for most migrations. Some migrations might require a pre-push to live to deal with the database changing, but I usually find I'm adding columns way more often than I'm deleting them, and renaming could be a "add column, copy data, remove old column on next deploy"

Sock on a Fish
Jul 17, 2004

What if that thing I said?

prom candy posted:

Have you looked into Capistrano? Your set up is a little more complicated than mine but if there's someone out there who's solved your particular problem it's fairly likely that they've done it with Capistrano.

Maybe I'm not well acquainted enough with it, but it seems like Capistrano wouldn't be appropriate for auto-scaling groups. Capistrano pushes out updates, but auto-scaling hosts need to pull the assets they need when they get provisioned. If you do a cap deploy and a host gets added to the group during the deploy, the cap deploy would miss it.

Pardot
Jul 25, 2001




enki42 posted:

Currently the process I'm thinking of testing out that hopefully should make things completely seamless:

This is a really interesting idea. If you give it a try let me know how it goes.

The new free database platform in beta has direct psql access, so you'd be able to run migrations locally docs. You'd also be interested in one of my coworker's post on zero downtime migrations.

hmm yes
Dec 2, 2000
College Slice
Thanks for posting those links. Good stuff.

Lamont Cranston
Sep 1, 2006

how do i shot foam

Look Around You posted:

That being said, if you want to really make yourself a "better" programmer, I'd recommend learning a functional language like Haskell or Lisp. They're really awesome languages in general, and functional programming is a good way to get a better grasp of recursion (especially considering that you don't really have loops at all!). It's also just good to step out of your comfort zone sometimes. A good book for learning Haskell is Learn You a Haskell for Great Good by fellow goon Bonus. Lisp has a couple good books too, notably SICP and How to Design Programs, which is sort of SICP lite.

Just wanted to say thanks for this. I've always wanted to explore a functional language and I've been working through the Haskell book; fascinating stuff.

Pardot
Jul 25, 2001




For those of you I sent the alpha of the postgres os x tool, thanks for the feedback. It's been released now so check it out http://inductionapp.com/

Physical
Sep 26, 2007

by T. Finninho
So here is a thing. After I do a pull in the morning I run "rake db:migrate: and get the following error

rake aborted!
no such file to load -- lib/acts_as_archive/base

Here is the line in the gemfile:
gem 'acts_as_archive', :git => 'http://github.com/xxx/acts_as_archive.git'

If I instead change it to this (commenting out the :git stuff) it rakes successfully.
gem 'acts_as_archive'#, :git => 'http://github.com/xxx/acts_as_archive.git'

What gives?

prom candy
Dec 16, 2005

Only I may dance
Shot in the dark, but try


gem 'acts_as_archive', :git => 'http://github.com/xxx/acts_as_archive.git', :require => 'acts_as_archive'

Alternately, go harass whoever pushed that change as they probably already figured out a fix.

prom candy fucked around with this message at 15:45 on Mar 7, 2012

Physical
Sep 26, 2007

by T. Finninho

prom candy posted:

Shot in the dark, but try


gem 'acts_as_archive', :git => 'http://github.com/xxx/acts_as_archive.git', :require => 'acts_as_archive'

Alternately, go harass whoever pushed that change as they probably already figured out a fix.
They are on linux and haven't had a problem. :negative:

prom candy
Dec 16, 2005

Only I may dance
That seems to be a theme in this thread for you.

Edit: Seriously, tell your work to set you up with a proper dev. environment. Alternately, tell your linux lubbin coworkers to put linux only gems into :group => :linux and then install your gems with

bundle install --without linux

Physical
Sep 26, 2007

by T. Finninho
But what is the problem? Is it trying to checkout a repo? It says that it installs.

prom candy
Dec 16, 2005

Only I may dance
He's specified that it should use the Gem found at that GitHub location, and that version of the gem is breaking something in your app. Run the rake task with --trace and you can figure out where it's blowing up.

Breaking your co-workers' poo poo with one of your commits is generally considered a faux pas, he should be helping you resolve this.

Strong Sauce
Jul 2, 2003

You know I am not really your father.





My company is trying to transfer a Rails app over from MySQL to an Oracle database. Unfortunately due to the way Rails creates table names, there is a table name that is 37 characters in length. Oracle only allows for 30 character object names.

Renaming the table / renaming the class object is currently out of the question since it is part of an open source project.

Is there anyway in Rails to specify that when table X is being referenced it should actually look at table Y rather than the rails converted name for table X?

Sharrow
Aug 20, 2007

So... mediocre.
set_table_name or self.table_name depending on version. (Does the latter work in older rails versions?)

https://github.com/rails/rails/commit/0b72a04

Fangs404
Dec 20, 2004

I time bomb.
I posted this on StackOverflow, but I haven't really had a response, so I figured I'd ask here. This problem has just really stumped me.

I'm maintaining an ancient RoR site that we're in the process of rewriting in Django. The site was written by someone else when Rails was in its infancy, and no one kept it updated until I got to it. A night or two ago, the server went down, I suspect due to a MySQL update. In the process of trying to fix it, we broke it, and now mongrel won't start. I see this in the mongrel log:

code:
    /home/USER/rails/SITE/config/boot.rb:26:Warning: Gem::SourcUSERdex#search support for String patterns is deprecated, use #find_name
    /usr/local/lib/site_ruby/1.8/rubygems.rb:812:in `report_activate_error': RubyGem version error: rails(1.2.3 not >= 3.0) (Gem::LoadError)
    	from /usr/local/lib/site_ruby/1.8/rubygems.rb:223:in `activate'
    	from /usr/local/lib/site_ruby/1.8/rubygems.rb:258:in `activate'
    	from /usr/local/lib/site_ruby/1.8/rubygems.rb:257:in `each'
    	from /usr/local/lib/site_ruby/1.8/rubygems.rb:257:in `activate'
    	from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:33:in `require'
    	from /home/USER/rails/SITE/config/environment.rb:24
    	from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:29:in `gem_original_require'
    	from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:29:in `require'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:147:in `rails'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:113:in `cloaker_'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:149:in `call'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:149:in `listener'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:99:in `cloaker_'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:50:in `call'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:50:in `initialize'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:84:in `new'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:84:in `run'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/command.rb:212:in `run'
    	from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281
    	from /usr/local/bin/mongrel_rails:19:in `load'
    	from /usr/local/bin/mongrel_rails:19
I searched around and have tried to diagnose the error. It looks like mongrel wants us to have rails 3+ which isn't possible because we're going to be replacing the site in just a few weeks and don't want to bother bringing it up to date (it just needs to *work* for now). How can I force mongrel to run with the current version of rails?

In `config/environment.rb`, we have `RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION` which I thought would force Rails 1.2.3. Also, here's the output of `gem list`:

code:
    *** LOCAL GEMS ***
    
    abstract (1.0.0)
    actionmailer (3.2.2, 3.0.3, 1.3.3)
    actionpack (3.2.2, 3.0.3, 1.13.6, 1.13.3)
    actionwebservice (1.2.6, 1.2.3)
    activemodel (3.2.2, 3.0.3)
    activerecord (3.2.2, 3.0.3, 1.15.6, 1.15.3)
    activeresource (3.2.2, 3.0.3)
    activesupport (3.2.2, 3.0.3, 1.4.4, 1.4.2)
    acts_as_ferret (0.5.3, 0.4.3)
    arel (3.0.2, 2.0.7)
    builder (3.0.0, 2.1.2)
    bundler (1.1.0, 1.0.9)
    cgi_multipart_eof_fix (2.5.0)
    daemons (1.1.8, 1.1.0)
    erubis (2.7.0, 2.6.6)
    fastthread (1.0.7)
    ferret (0.11.6)
    gem_plugin (0.2.3)
    hike (1.2.1)
    i18n (0.6.0, 0.5.0)
    jk-ferret (0.11.8.3, 0.11.8.2)
    journey (1.0.3)
    json (1.6.5)
    mail (2.4.3, 2.2.15)
    mime-types (1.17.2, 1.16)
    mongrel (1.1.5)
    multi_json (1.1.0)
    polyglot (0.3.3, 0.3.1)
    rack (1.4.1, 1.2.1)
    rack-cache (1.2)
    rack-mount (0.8.3, 0.6.13)
    rack-ssl (1.3.2)
    rack-test (0.6.1, 0.5.7)
    rails (1.2.3)
    railties (3.2.2, 3.0.3)
    rake (0.9.2.2, 0.8.7)
    rdoc (3.12)
    sprockets (2.1.2)
    thor (0.14.6)
    tilt (1.3.3)
    treetop (1.4.10, 1.4.9)
    tzinfo (0.3.32, 0.3.24)
Thanks for any and all help!

Fangs404 fucked around with this message at 20:01 on Mar 14, 2012

Strong Sauce
Jul 2, 2003

You know I am not really your father.





Fangs404 posted:

I posted this on StackOverflow, but I haven't really had a response, so I figured I'd ask here. This problem has just really stumped me.

I'm maintaining an ancient RoR site that we're in the process of rewriting in Django. The site was written by someone else when Rails was in its infancy, and no one kept it updated until I got to it. A night or two ago, the server went down, I suspect due to a MySQL update. In the process of trying to fix it, we broke it, and now mongrel won't start. I see this in the mongrel log:

code:

In `config/environment.rb`, we have `RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION` which I thought would force Rails 1.2.3. Also, here's the output of `gem list`:
Thanks for any and all help!


Well if its possible that RAILS_GEM_VERSION is already defined as 3.0, then it won't set it to 1.2.3.

Also try running the Rails app through Webrick and see if its actually Mongrel or a Rails problem. This looks like a Rails/Gem issue.

Fangs404
Dec 20, 2004

I time bomb.

Strong Sauce posted:

Well if its possible that RAILS_GEM_VERSION is already defined as 3.0, then it won't set it to 1.2.3.

Also try running the Rails app through Webrick and see if its actually Mongrel or a Rails problem. This looks like a Rails/Gem issue.

I tried getting rid of the unless defined? RAILS_GEM_VERSION in environment.rb, but that didn't fix it. How would I define RAILS_GEM_VERSION to mandate 1.2.3?

I'll see if I can figure out Webrick. Thanks for the suggestion.

[edit]
OK, I figured it out. It was incompatible gem(s). Luckily, we have a test server and a production server. We only messed with the test server last night (mongrel on the production server was working), so I did `gem list` on both servers, and it turned out the test server had quite a different list. I just installed and uninstalled gems on the test server until `gem list` produced identical lists on both servers.

Fangs404 fucked around with this message at 21:28 on Mar 14, 2012

Vivian Darkbloom
Jul 14, 2004


I'm trying to put together a short demo for cognitive science grad students on why Ruby is a useful tool to have around. Most of these folks do only a moderate amount of coding and know some Python and miscellaneous other languages.

What are some features that would be impressive to show off? I was planning to show a little script I made for data processing and go over some of the cool features, like adding methods to built-in classes, closures, etc. I know there's not really a slam-dunk case for Python over Ruby but for me Ruby is the clear winner, even with less library support available.

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.

Vivian Darkbloom posted:

I'm trying to put together a short demo for cognitive science grad students on why Ruby is a useful tool to have around. Most of these folks do only a moderate amount of coding and know some Python and miscellaneous other languages.

What are some features that would be impressive to show off? I was planning to show a little script I made for data processing and go over some of the cool features, like adding methods to built-in classes, closures, etc. I know there's not really a slam-dunk case for Python over Ruby but for me Ruby is the clear winner, even with less library support available.

One of my favourite things about Ruby is how its fully object-oriented approach makes operating on sequences easy. For example, say that you want to find the squares of prime numbers less than equal to 100.

Start off with a list of natural numbers from 1 to 100.

pre:
(1..100)
Only select prime numbers. (Assuming that we have an Fixnum#prime? method available, I'm not going to go into how to implement that.)

pre:
(1..100).select { |n| n.prime? } 
Square the found prime numbers.

pre:
(1..100).select { |n| n.prime? }.map { |n| n**2 }
I find this style easy to understand, as it allows me to write (or read!) the code in the same order as I think about the transformations. Compare this with Python, where you must work from right to left to achieve the same effect. (You could do this example with a single Python list comprehension, but they don't really work in more complicated examples, so I'm not going to do that.)

pre:
[n for n in range(1, 101)]
pre:
filter(lambda x: isprime(x), [n for n in range(1, 101)])
pre:
map(lambda x: x**2, filter(lambda x: isprime(x), [n for n in range(1, 101)]))
E:

One caveat though. Ruby's Enumerable module still has some issues. For example, methods like Enumerable#map return an array, which makes operating on large or even infinite sequences challenging, but fortunately, it is being worked on.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Fangs404 posted:

I tried getting rid of the unless defined? RAILS_GEM_VERSION in environment.rb, but that didn't fix it. How would I define RAILS_GEM_VERSION to mandate 1.2.3?

I'll see if I can figure out Webrick. Thanks for the suggestion.

[edit]
OK, I figured it out. It was incompatible gem(s). Luckily, we have a test server and a production server. We only messed with the test server last night (mongrel on the production server was working), so I did `gem list` on both servers, and it turned out the test server had quite a different list. I just installed and uninstalled gems on the test server until `gem list` produced identical lists on both servers.

What you can do in the future is set up bundler and a Gemfile, and when launching your app use bundler. I don't see why a particularly ancient Rails version would pose problems with this.

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled
Anyone else going to RubyNation next weekend?

plasticbugs
Dec 13, 2006

Special Batman and Robin
EDIT: I figured it out. My solution at the bottom - I didn't know about zip


My site allows users to add video games to a collection and makes use of the Amazon API.

The Games model has a string column called 'asin' (Amazon Product ID). When the User views their Games page, the Games controller creates an Array of all the @user.games ASINs and makes an Amazon API request using that array of ASINs.

To work with the response, I convert it to a Hash and pull the data I need like this:

@collection = response['Items']['Item']

So, if the user has 3 games, then @collection is an Array of 3 Hashes that I can iterate through (these hashes have things like Image URLs, Product Descriptions, Prices etc).

I'd like to give the user the ability to delete a game on the page that lists all their games.

My problem is this:
I have @user.games (an array of 3 games)
I also have @collection (an array of 3 amazon responses, that has no hooks back to my Games column)

How can I do something like what's below in my view?

@collection[0]['AwesomeAPIData'] | link to destroy item based on @user.games[0]
@collection[1]['AwesomeAPIData'] | link to destroy item based on @user.games[1]
@collection[2]['AwesomeAPIData'] | link to destroy item based on @user.games[2]

I have a solution in place that does what I want, where I'm using the Amazon Response to find the ASIN and then doing a reverse lookup with @user.games.find_by_asin(asin), but it's REALLY kludgy. I'd rather pass in an array of game objects from my controller and link them to the Amazon response somehow.

EDIT: More succinctly (with fake code), how can I:

@everything = [@amazon_hashes, @game_arrray]

@everything.each do | amazon, user_game_data|
amazon.foo | user_game_data.bar
end

SOLUTION:

@everything = @amazon_hashes.zip(@game_array)

@everything.each do | x, y|
x.foo
y.bar
end

plasticbugs fucked around with this message at 00:31 on Mar 18, 2012

Large Hardon Collider
Nov 28, 2005


PARADOL EX FAN CLUB
I'm brand new to rails. What's the "right way" to change just one field in a database table? I want to do

UPDATE ratings SET text="blahblah" WHERE text="balhblah"

I thought that the right way would be through generating a migration, but it looks like that's just for updating the structure of the database, rather than the contents (correct me if I'm wrong). How do I change this one field, besides doing it manually through dbconsole (which would screw up the versioning)?

prom candy
Dec 16, 2005

Only I may dance
Checkout ActiveRecord's update_all method. You're trying to update a number of records, correct?

Large Hardon Collider
Nov 28, 2005


PARADOL EX FAN CLUB

prom candy posted:

Checkout ActiveRecord's update_all method. You're trying to update a number of records, correct?
Actually just one. The site lists local businesses, each with a number of aspects you can rate, formulated as questions. The questions themselves are stored in the database, and I want to correct a typo in one of them.

Pardot
Jul 25, 2001




I'd just use execute to do the sql. Example: http://guides.rubyonrails.org/migrations.html#using-the-up-down-methods

prom candy
Dec 16, 2005

Only I may dance

Large Hardon Collider posted:

Actually just one. The site lists local businesses, each with a number of aspects you can rate, formulated as questions. The questions themselves are stored in the database, and I want to correct a typo in one of them.

So you're looking to do the most basic update operation then. Open up your console and do something like.
code:
id = <id of the rating>
rating = Rating.find(id)
rating.field = value
rating.save
Alternately:

code:
Rating.find(<id of the rating>).update_attributes(:field => value)
I don't mean to be a dick, but this is really basic stuff that you should have learned from a beginner's tutorial. It's going to be a long, slow learning process if you don't do some up-front reading on the basics.

Large Hardon Collider
Nov 28, 2005


PARADOL EX FAN CLUB

prom candy posted:

So you're looking to do the most basic update operation then. Open up your console and do something like.
code:

id = <id of the rating>
rating = Rating.find(id)
rating.field = value
rating.save
Alternately:

code:

Rating.find(<id of the rating>).update_attributes(:field => value)

I don't mean to be a dick, but this is really basic stuff that you should have learned from a beginner's tutorial. It's going to be a long, slow learning process if you don't do some up-front reading on the basics.
No offense taken. I should have mentioned that I'm working with git and separate staging and production servers, which is a first for me. Usually I'd manually edit the database, but I know that I need to do something in the rails app so it will be consistent across all our environments.

Also, the database was migrated from the old php setup, so the values in this table aren't derived from a seed file I can edit.

I thought that the appropriate way to do this was to make a db:migrate operation, but it seems like that's for changing database structures, not contents. Is that assumption wrong? Otherwise, I suppose I will make a custom rake task, but that means that I will have to manually run it in each environment, right?

Thanks for your help, guys. I started learning this stuff just two days ago, and I know I've got a lot to learn.

prom candy
Dec 16, 2005

Only I may dance
Migrations are for structure but you can put content operations in there as well. However, that's not really the best way to go.

I would put it in a rake task and then run it on your different environments, or just do it manually from the console on your environments. It depends on if you're going to need to reuse the task or not. Will somebody cloning the project for the first time have to deal with this, or is it a case of somebody entering bad data into a production DB?

Adbot
ADBOT LOVES YOU

Physical
Sep 26, 2007

by T. Finninho
Ok so this is kind of a rudimentry question but I am having a hard time dissecting it to do a fruitful google search.

I have a HighLazyChart object that I am trying to get the data array out of. Here is what I got so far.


#<LazyHighCharts::HighChart:0x7c22340 @collection_filter=nil, @data=[{:name=>"Downtime Impact Rating", :data=>[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]}], @options={:title=>{:text=>"", :x=>20}, :legend=>{:enabled=>false}, :xAxis=>{:categories=>["1 Hour", "4 Hours", "12 Hours", "1 Day", "2 Days", "3 Days", "1 Week", "2 Weeks", "3 Weeks", "4 Weeks"]}, :yAxis=>{:title=>{:text=>"Impact Rating"}, :min=>0, :max=>5, :tickInterval=>1}, :tooltip=>{:enabled=>true}, :credits=>{:enabled=>false}, :plotOptions=>{:areaspline=>{}}, :chart=>{:height=>150}, :subtitle=>{:text=>"", :x=>20}}, @html_options={:style=>"height: 300px, width:615px"}, @placeholder="graph">


[{:name=>"Downtime Impact Rating", :data=>[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]}]


The code that generates that output is the following too lines
<%= @blah.inspect %>
<%= @blah.data.inspect %>

Now, I want access to the :data array in the @blah.data.inpsect line, the one with the 3,3,3,3,3 values in it. But @blah.data.data doesn't work and neither does @blah.data[:data] How the hell do I get access to that and also how do I do this in the future? How do I understand what the inspect dump is displaying and how to access the object data?

edit: woops broke a table by using the code tags

Physical fucked around with this message at 15:39 on Mar 20, 2012

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply