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
Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
The app I'm working on has somehow gotten itself into a bizarre state. I created a migration to get rid of two now defunct columns:

Ruby code:
class DropUnusedColumns < ActiveRecord::Migration
  def change
    remove_column :challenges, :height
    remove_column :challenges, :width
  end
end
However, now when I run our task to drop, create and reseed the database

code:
bundle exec rake db:drop db:create db:migrate db:seed db:test:prepare
I get a SQL error:

code:
==  DropUnusedColumns: migrated (0.0835s) =====================================

Seeding database...
rake aborted!
Mysql2::Error: Unknown column 'height' in 'field list': INSERT INTO `challenges` ....
There is absolutely no mention of the height field anywhere in the app - seeds.rb uses FactoryGirl to load seed data, and there is no height being set anywhere there. Hell, even if I do a recursive grep of the entire app, there is no height being set anywhere with respect to challenges.

It's almost as if rake is using a weird cached version of the model, and not the actual one. Any thoughts?

Adbot
ADBOT LOVES YOU

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

The Journey Fraternity posted:

If you're dropping the entire database to reseed it, why not just load the database from schema?

code:
rake bundle exec rake db:drop db:create db:schema:load db:seed db:test:prepare

Yeah, fair point. I'm new to this team, and that's what they all do as a matter of course.

Anyway, a guy on our team found the solution to this: FactoryGirl needs to be reloaded:

Ruby code:
  ActiveSupport::Dependencies.clear
  FactoryGirl.reload

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
^^ Is MySql workbench really that bad? I've never used it, but assumed it was the de facto standard tool.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Lexicon posted:

^^ Is MySql workbench really that bad? I've never used it, but assumed it was the de facto standard tool.

I've just inherited a rails app and for some reason the browser history is totally broken. You can navigate around the site just fine, but the URL in the URL bar never changes to anything other than the site root. I'm not really a javascript guy, but I'm assuming there's some javascript fuckery going on here. Can anyone point me in the right direction for sorting this out?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
I want to build a dashboard type thing in rails that will run some SQL queries on a database (not the rails persistence database - a business database elsewhere that already exists), and then display charts of the results in the web UI.

Any recommendations for a technique to do this? I was thinking of using google's chart API.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

A MIRACLE posted:

Active Admin + your favorite SVG based javascript graphics library.

Is Active Admin a good way to run arbitrary SQL on an unrelated database though?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
Following on from my above question, I've found a way to use ActiveRecord infrastructure to make connections to a non-rails database:

code:
class NonRailsDb < ActiveRecord::Base
  establish_connection(:prod_logging_database)
  
  def self.get_objects
    self.connection.select_value("SELECT COUNT(*) FROM users")  # Put arbitrary SQL here.
  end
end
The problem with this is that I cannot create an instance of NonRailsDb, because ActiveRecord assumes a table exists with that name. Can I circumvent this behaviour? I want to use this class as a view model of sorts... but I don't want to have to create a database table to fool rails.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
I'm building a rails app which will be hosted on Heroku and pulls data from an RDS MySql connection. Note that this connection is in addition to the Postgres connection to the database that's the persistence layer for the rails app itself.

I've been able to authorize Heroku to connect to the RDS instance; however, it replaces CONFIG['DATABASE_URL'] with the mysql connection string, which is not a huge problem in itself.

The thing is, the value stored there is a Mysql2 connection string: e.g. mysql2://user:pass@....

I want to use the mysql2 gem to connect to this database, and Mysql2::Client.new takes a hash of parameters. Short of doing a regex to pull out these parameters into a hash, is there a way to connect using a connection string?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Pardot posted:

You can use the URI stdlib

Ruby code:
require 'uri'
URI.parse('mysql2://user:pass@hostname.com/dbname').tap {|a| p [a.scheme, a.user, a.password, a.host, a.path[1..-1]] }
["mysql2", "user", "pass", "hostname.com", "dbname"]

Worked perfectly. Thanks!

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
Has anyone here tried to pull data from the google analytics API from within a rails app? It seems like an utter clusterfuck with a bunch of defunct gems to interface with it, and Google's own ruby library in alpha.

I can't find half decent sample code anywhere on the web :(

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

kitten smoothie posted:

Do you want to spend time computer janitoring a server, or do you want to spend that time writing code that could make you money and deal with migration if it takes off?

Seriously, this.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Obsurveyor posted:

I just did some learning about AngularJS last week and it seems to me that if you're going to embrace Angular, you don't need full stack Rails. Angular is going to cover your views and controllers. Just go with Sinatra or something for JSON services that give you objects.

As a long time server-side MVC fan (and fairly recent Rails convert), the idea of client-side controllers makes zero sense to me whatsoever. Would you mind explaining the rationale behind it?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Obsurveyor posted:

"Presenter" would probably be more accurate than controller, though that's what AngularJS calls them. Like Smol said, you can have both server-side and client-side stuff and it really just depends on what exactly you're doing. However, if it's heavy AJAX and lots of dynamic page updates or a single page app, server side controllers kind of go away for the most part, as far as I can see. Lots of stuff from Rails just goes away for single page apps and all you really need is something to save and retrieve objects. You don't need a full Rails stack for that.

That Lost Architecture presentation by Bob Martin(he's not my uncle) kind of addresses but doesn't drive home hard enough: Too much of ruby web development focuses on Rails being the star of the show when it should be what the application is doing. I know it has been really, really hard for me to not think of web development as being centered around what the Rails app is doing with all the Rails trimmings and machinery and stuff and the rest of the application and what it does just mixed in.

Cool, makes sense. I'll check out that presentation. Thanks.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
I've always meant to ask this, but never got around to it... what exactly is going on within the respond_to block in this code:

code:
respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @users}
    format.json { render :json => @users}
end
Obviously I know what the outcome of this is, but the calling syntax is bizarre. format.xml or format.json appear to be method calls, but with a conditional baked in? As in, they only execute if the format is indeed xml.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Obsurveyor posted:

Yeah, I don't like a lot of the magic that is built-in to this stuff. I think it gets even more crazy than this. Those methods are passing blocks, I think. It could be re-written:

code:
respond_to do |format|
    format.html # index.html.erb

    format.xml do
      render :xml => @users
    end

    format.json do
      render :json => @users
    end
end

That makes a bit more sense, but I guess there's still an implicit conditional? As in the json() method of format only yields if request.path terminates in .json or something?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
I have a table with pre-existing data in MySql used by a rails app. I want to add a new column (article_slug) that needs to be unique and ideally also has a database index for fast lookup. The pre-existing data needs to remain there, but it won't ever be affected by the new logic.

Is there a reasonable way to have my constraint and index only apply to *new* records, or do I have to go back and make an article_slug for the old entries?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
Thanks, worked great!

On an unrelated note, is it considered bad form for a mixin to presume the existence of methods/variables in classes which include it? e.g.

code:
module MyMixin
  def printy
    puts self.foo
  end
end

class MyClass
  include MyMixin

  def foo
    "foooooo"
  end
end
Here this mixin would fail if MyClass didn't define foo(), and you tried to call printy(). Is it bad form to presume existence like this, and if not, is there a common pattern for validating the existence of needed methods at the time of include (rather than later at the time of use)?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

DankTamagachi posted:

Thank you all for the help here! I noticed this morning the same thing you did- simple math meant that if I was to_i'ing to INTs all the time, the rating bottomed out at 1 pretty quickly. D'oh!

I'm curious as to why you mentioned that this should be a "model function." I'm still getting used to this whole MVC framework, and the way I've been operating is basically putting only short attribute accessor-type functions in my models and all real methods in the controllers.

When should I put things in the model and when in the controller? Is there a good rule of thumb for stuff like this?

Golden rule of MVC is: Fat models, thin controllers.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
Not sure if this is the best place for this question, but here goes: I recently jumped ship from MacVim to Sublime Text 2, as the vintage (i.e. vim) mode is now close enough to vim for me, and it's a much nicer editor as a whole.

One complaint: the javascript syntax highlighting isn't quite up to par - for example, javascript dicts don't have the keys displayed in a different color the way they do in MacVim with Janus. Is there a way to fix this?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
^ Ok will do, thanks.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Smol posted:

There is no need to make it more complicated than it has to be. For loop also reads better and is easier to type. #each is the right tool when you want to do a side-effect after a series of transformations.

Your entitled to your opinion, but you are at odds with virtually the entire ruby community. each {} is the standard way to iterate over a collection, and for-loops look downright bizarre in ruby code. I've never even written one.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Lexicon posted:

Your entitled to your opinion, but you are at odds with virtually the entire ruby community. each {} is the standard way to iterate over a collection, and for-loops look downright bizarre in ruby code. I've never even written one.

Ok, I have far less conviction in this statement if DHH is a for-loop guy...

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
What's preferable?

a = Hash.new or a = {}

I'm a python refugee and am biased towards the latter, but I see both. Is there a majority opinion on this?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Smol posted:

The latter. Only use Hash.new when you need to use one of the special constructors for providing default values or something similar.

The same applies for Arrays as well.

Great, thanks. Every time I create one, I wonder which to use. This finally can be put to rest.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Daynab posted:

Hi, I'm interested in learning Ruby and then RoR but I had a question and there's no general Ruby thread - how useful is plain standalone Ruby to code programs compared to say... Python? Is it versatile or is it more "the language that Rails just happens to run on"?

As a data point, I used Ruby extensively for scripting, parsing, automation etc long before I ever got into rails.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
This should be super easy, but I can't quite work it out: I want to create a simple form in rails that's not coupled to an ActiveRecord model. I simply want to hit a controller, and then I'll deal with the posted params from there. However, when I click my button, nothing happens - my controller never gets hit. The route is properly set up.

Ruby code:
%form{:class => "form-inline", :action => "/blar", :method => "post"}
  %label
    From:
  %input{:type => "text", :placeholder => "yyyy-mm-dd", :class => "input-small datepicker"}
  %label
    To:
  %input{:type => "text", :placeholder => "yyyy-mm-dd", :class => "input-small datepicker"}
  %button{:type => "button", :class => "btn btn-primary"}
    Display

What am I missing here?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Physical posted:

Is it the difference between button and submit_tag? Or is your form tag crossing any div tags?

Just tried submit_tag - same result. And I'm not sure what you mean by the latter point. The HAML is well formed, I'm pretty sure.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
I've been using Sublime Text 2 for rails for a few weeks, and generally love it. However, after rebooting my machine today, its ruby linter is acting up. It is complaining about things like this:

Ruby code:
def challenges_query(params={})
  p = { id:         11,
        start_date: 7.days.ago.strftime('%F'),
        end_date:   Time.now.strftime('%F') }.merge(params)
  result = do_stuff_with_p(p)
  result    
The linter highlights the definition of p, and says "Odd number list for Hash; Syntax error, unexpected ':'".

Any idea what the hell is going on?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Civil Twilight posted:

The linter is checking your code with ruby 1.8 (probably the default system ruby), which doesn't have that hash syntax. Make sure you're getting the right version of ruby at the command line, and you might need to configure SublimeLinter to use a specific ruby instead of the default.

Yeah, that makes sense, however:

code:
$ which ruby
/Users/me/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
Also, when I set SublimeLinter's ruby executable explicitly:

code:
{
  "sublimelinter_executable_map":
  {
    "ruby": "/Users/me/.rvm/rubies/ruby-1.9.3-p0/bin/ruby"
  }
}
I get the same problem with the linter. Very weird...

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Dangerllama posted:

This sounds like teaching Linear Algebra before someone's taken Algebra I and II.

No kidding. Pardot - do yourself a favour and put Rails aside until you're comfortable with Ruby and universal programming constructs such as looping, objects, etc. I'm an experienced software engineer and I find Rails to be a big beast to handle at times.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

enki42 posted:

I find starting 100% with Ruby just gets people frustrated that they're not actually making something useful. Plus if you're just going with pure Ruby, people won't be motivated to experiment with stuff outside of direct exercises you give them.

Devil's advocate: Why not base a course off something like projecteuler.net (or equivalent - adjust for your audience's interests)? I find that people are surprisingly intrigued to find out that programming opens up a whole world of problem solving that was previously inaccessible.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
Can someone explain or point me towards an overview of the threading model in rails? I'm curious in particular about the performance implications of server side network requests or file reads, etc.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Smol posted:

Short answer: don't do it, unless you're on JRuby. And even then, it depends. What version of Rails are you running?

3.2.11

How would one handle an image proxy, say, in a rails app if server side requests are not a good idea?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
This guy paints a particularly dire picture of the Rails security situation right now:

http://www.kalzumeus.com/2013/01/31/what-the-rails-security-issue-means-for-your-startup/

How much of this is bluster and how much should be taken seriously? Is Rails really that much worse than other environments, e.g. PHP?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
Thanks; that's a very well-reasoned and rational point of view. I've only been involved in rails in earnest for about 4 months - I love it unreservedly, and want to use it for some future project ideas, but this recent security flap has given me some pause.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Smol posted:

Also, remember how much Rails does by default to protect you. Do you know what an IP spoofing attack is? Or how to use cryptographic signatures in a way that does not expose you to timing attacks? What characters do you need to escape to prevent XSS vulnerabilities? Exactly.

Very true.

Today I was wondering: is the dynamic nature of ruby something that necessarily poses risks within rails (or python and django) that wouldn't occur in a statically typed language? Java, for example, is truly horrendous to work with, but I imagine its a lot harder to actually inject malicious code. Am I off base here?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
Another security question: I'm working on an app where users are asked to enter a zip code, and this is posted to a rails route, and eventually entered into the Submission.zipcode model attribute in the database.

It goes without saying that I'll be checking for valid zipcode-ness on the client side. What about on the server side, as someone could construct a malicious post if they were so inclined. I assume ActiveRecord has some degree of SQL injection protection, but what's the best practice here? It's easy to use model validation in the case of a zip code, but what about an arbitrary text field?

Phrased differently, what's an appropriate level of paranoia?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

dexter posted:

ActiveRecord will handle escaping the data for you along with validating that it is a string unless you do something stupid like Model.executes("INSERT INTO `table.... It's up to you to enforce further constraints on the data that it's actually a valid ZIP code.

Validating the format is easy in ActiveRecord with validations; using a combination of length and numericality gets you most of the way there. Actually verifying that it's a valid ZIP code requires an up to date list of ZIP codes.

Thanks... That confirms everything I thought. Just wanted to be sure I wasn't missing something important.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

prom candy posted:

Update Rack if you're using it, more security fun: http://rack.github.com/

What exactly is rack? I've read the overview page on github and I'm not really any the wiser.

Adbot
ADBOT LOVES YOU

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Smol posted:

Dynamically typed frontend and a weakly typed db is a recipe for trouble. :smith:

Why does weak typing exist, like, at all? It seems to do nothing but cause huge headaches, often of the security variety.

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