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
Deus Rex
Mar 5, 2005

Cocoa Crispies posted:

It's also how Object#BigDecimal() works, strangely:

code:
0 :> Kernel.BigDecimal "fart"
     # => #<BigDecimal:7ffa7110d740,'0.0',9(9)>
0 :> Kernel.Float "fart"
ArgumentError: invalid value for Float(): "fart"
	from (irb):25:in `Float'
	from (irb):25
	from /Users/bkerley/.rbenv/versions/1.9.2-p290/bin/irb:12:in `<main>'

:wtc: who put PHP in my Ruby?? :(

quote:

initial:
The initial value, as an Integer, a Float, a Rational, a BigDecimal, or a String. If it is a String, spaces are ignored and unrecognized characters terminate the value.

code:
[1] pry(main)> BigDecimal "fart"
=> #<BigDecimal:7fa43a19c4c8,'0.0',9(9)>
[2] pry(main)> BigDecimal "5fart"
=> #<BigDecimal:7fa43a2988b8,'0.5E1',9(18)>

Adbot
ADBOT LOVES YOU

Jam2
Jan 15, 2008

With Energy For Mayhem
I hacked together a class method to search for models by matching query tokens to substrings of attributes in my model. Is there any way to rewrite this in a way that would permit me to chain scopes on it? I am running into issues trying to tack on will_paginate.

Ruby code:
  def self.full_name_search(str)
    guests = self.scoped
    return guests if str.nil? || str.empty?
    tokens = str.downcase.split
    guests.select! do |guest|
      keep = true
      tokens.each do |token|
        unless guest.first_name.downcase.include?(token) || guest.last_name.downcase.include?(token)
          keep = false
        end
      end
      keep
    end
    guests
  end

prom candy
Dec 16, 2005

Only I may dance
Why not use your database's searching features rather than making Ruby do it? That code is going to get awfully slow once your database fills up as as soon as you run guests.select! it's going to "SELECT * FROM guests" in the background. If you're using postgres pg_search (https://github.com/Casecommons/pg_search) is pretty good, and there are a lot of full text search gems/plugins for MySQL too (ferret, SOLR). At this point I think even using LIKE would be preferable to how you've got it set up.

Deus Rex
Mar 5, 2005

if anyone's interested in seeing what a very barebones Rack middleware gem looks like, I wrote one last night: https://rubygems.org/gems/rack-city (github: http://github.com/tomjakubowski/rack-city)

epswing
Nov 4, 2003

Soiled Meat
Hi folks! I'm updating Redmine from 1.3.1 to 2.0.3, which involves updating rails 2 to rails 3, among other things.

My current installation is actually a bitnami redmine stack on windows. It installed fine and has been working great for a year now.

I think my options are

(A) try to update rails and rake and all the other things that redmine 2.0.3 requires, and then work my way through the redmine upgrade guide.

(B) store a mysqldump of the database, uninstall the bitnami redmine 1.3.1 stack, install the bitnami redmine 2.0.3 stack (which will have all the necessary dependencies), restore the database, and run "rake db:migrate RAILS_ENV=production" to update the 1.3.1 database to the 2.0.3 schema.

Which one seems like less of a headache?

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

epalm posted:

Hi folks! I'm updating Redmine from 1.3.1 to 2.0.3, which involves updating rails 2 to rails 3, among other things.

My current installation is actually a bitnami redmine stack on windows. It installed fine and has been working great for a year now.

I think my options are

(A) try to update rails and rake and all the other things that redmine 2.0.3 requires, and then work my way through the redmine upgrade guide.

(B) store a mysqldump of the database, uninstall the bitnami redmine 1.3.1 stack, install the bitnami redmine 2.0.3 stack (which will have all the necessary dependencies), restore the database, and run "rake db:migrate RAILS_ENV=production" to update the 1.3.1 database to the 2.0.3 schema.

Which one seems like less of a headache?

They both sound like a PITA, but I'd probably just follow the guide. Are you using Pik? It might not apply in this situation but I found it helpful when I developed on Windows. (https://github.com/vertiginous/pik)

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled
I get this error when running resque-web. I've tried rebooting, reinstalling resque and vegas and recompiling ruby. This started happening after I ran out of hard drive space while a resque job was writing to a file. I made some free space but resque-web hasn't started since. I've tried on ruby 1.9.2 and 1.9.3 and the same error happens:

quote:

dan@ubuntu:~$ resque-web
[2012-09-10 22:34:02 -0400] Starting 'resque-web'...
/home/dan/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/open-uri.rb:35:in `initialize': No such file or directory - (Errno::ENOENT)
from /home/dan/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/open-uri.rb:35:in `open'
from /home/dan/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/open-uri.rb:35:in `open'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/gems/vegas-0.1.11/lib/vegas/runner.rb:146:in `port_open?'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/gems/vegas-0.1.11/lib/vegas/runner.rb:160:in `check_for_running'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/gems/vegas-0.1.11/lib/vegas/runner.rb:108:in `start'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/gems/vegas-0.1.11/lib/vegas/runner.rb:77:in `initialize'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/gems/resque-1.22.0/bin/resque-web:13:in `new'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/gems/resque-1.22.0/bin/resque-web:13:in `<top (required)>'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/bin/resque-web:19:in `load'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/bin/resque-web:19:in `<main>'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:13:in `eval'
from /home/dan/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:13:in `<main>'
Any ideas?

prom candy
Dec 16, 2005

Only I may dance
Is there such a file or directory?

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled

prom candy posted:

Is there such a file or directory?
I can't figure out what file or directory it's looking for. It seems to be checking to see if a port is open, which makes sense since resque-web needs to listen on a port.

Cock Democracy fucked around with this message at 15:55 on Sep 11, 2012

prom candy
Dec 16, 2005

Only I may dance
What's going on in /home/dan/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/open-uri.rb at line 35?

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled

prom candy posted:

What's going on in /home/dan/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/open-uri.rb at line 35?

Here it is. Line 35 is commented:

code:
  def open(name, *rest, &block) # :doc:
    if name.respond_to?(:open)
      name.open(*rest, &block)
    elsif name.respond_to?(:to_str) &&
          %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
          (uri = URI.parse(name)).respond_to?(:open)
      uri.open(*rest, &block)
    else
      open_uri_original_open(name, *rest, &block)  # line 35
    end
  end

prom candy
Dec 16, 2005

Only I may dance
Huh... I know that open-uri will try to open local files under certain circumstances, but I'm not sure in this situation why it would be doing that.

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
Delete the resque_web.url file. It's likely empty right now, as it couldn't write to it when the disk is full.

E: It's likely in ~/.vegas/resque_web.url

E2: And if that's not it, why don't you fire up a debugger and see what it is trying to open? It's a lot easier to debug errors like this by simply checking yourself what the code is actually doing.

Smol fucked around with this message at 16:23 on Sep 11, 2012

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled

Smol posted:

Delete the resque_web.url file. It's likely empty right now, as it couldn't write to it when the disk is full.

E: It's likely in ~/.vegas/resque_web.url
That worked. Thanks a ton! I had been trying to figure that out since last week. I was about to break down and configure a new VM for it.

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.

Cock Democracy posted:

That worked. Thanks a ton! I had been trying to figure that out since last week. I was about to break down and configure a new VM for it.

Come on man, since last week? You shouldn't be afraid to read library code (in fact, I'd argue that one should read the code for the libraries that they're using, if possible). Let me show you what what I did to solve your problem so the next time a similar problem comes up, you can fix it yourself.

In the stacktrace you can see that open was called from some method called port_open?. Let's open up lib/vegas/runner.rb and see what it looks like.

Ruby code:
    def port_open?(check_url = nil)
      begin
        check_url ||= url 
        options[:no_proxy] ? open(check_url, :proxy => nil) : open(check_url)
        false
      rescue Errno::ECONNREFUSED, Errno::EPERM, Errno::ETIMEDOUT
        true
      end 
    end 
It's trying to open something called check_url which was given as a parameter to the method. If we look at the stack trace again we see that port_open? was called on line 160 in a method called check_for_running.

Ruby code:
    def check_for_running(path = nil)
      if File.exists?(pid_file) && File.exists?(url_file)
        running_url = File.read(url_file)
        if !port_open?(running_url)
          logger.warn "#{quoted_app_name} is already running at #{running_url}"
          launch!(running_url, path)
          exit!(1)
        end
      end
    end
In check_for_running we can see that the url comes from the contents of some file called url_file. Okay, what's url_file?

Ruby code:
    def url_file
      options[:url_file] || File.join(app_dir, "#{filesystem_friendly_app_name}.url")
    end
We're getting close. Just need to find out where app_dir is and perhaps check how exactly filesystem_friendly_app_name differs from the normal app name.

Ruby code:
   def app_dir
      if !options[:app_dir] && !ROOT_DIR
        raise ArgumentError.new("nor --app-dir neither EVN['HOME'] defined")
      end
      options[:app_dir] || File.join(ROOT_DIR, filesystem_friendly_app_name)
    end
Aha. So app_dir is just ROOT_DIR dir plus file_system_friendly_app_name. Let's check out where they are defined.

Ruby code:
    ROOT_DIR = ENV['HOME'] ? File.expand_path(File.join('~', '.vegas')) : nil

   # ... code between

   @filesystem_friendly_app_name = @app_name.gsub(/\W+/, "_")
And that's it. filesystem_friendly_app_name is just the app's name with all non-word characters replaced with underscores and that by default, it resides in ~/.vegas/.

So I guess what I'm trying to say is that, with just a bit of courage, problems like this are very easy to solve by yourself. Don't be afraid to read code that you haven't written yourself. :unsmith:

Smol fucked around with this message at 10:34 on Sep 17, 2012

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled
Thanks Smol. I'll keep that in mind and will dig a little deeper next time.

Princess Kakorin
Nov 4, 2010

A real Japanese Princess
I want to get into this Rails thing, but this is so daunting. Should I just master Ruby first before attempting Rails, or keep on with trying Ruby and RoR at the same time?

Oh My Science
Dec 29, 2008
I started learning rails & ruby at the same time, so it's possible. A few months back I decided to focus more so on ruby in order to better understand what rails does, and I have to say it makes my life easier.

I signed up for the pragmatic online ruby course, which proved to be a decent resource. At this point I would like to dig deeper, not sure where I should look though.

Pardot
Jul 25, 2001




You might want to check out Sinatra first and build something simple to get your feet wet.

prom candy
Dec 16, 2005

Only I may dance
Ruby and Rails at the same time is how I learned. It was confusing at first but I think the benefit of being able to build useful software really quickly outweighed the drawback of not being clear on what was Ruby and what was Rails.

Jam2
Jan 15, 2008

With Energy For Mayhem
I'm having a database issue.

At some point, I introduced a migration to add a full_name string to my user model. This was applied to the production database and now the column exists in the app_production db. Around this time, I had some issues with migrations and had to delete the migration that introduced this column. So, now I have a column with no migration on record that explains how it got there. User.full_name is kind of like an orphan in that way.

I just set up a capistrano task to backup my production db and apply it to my local development environment. Now, for some reason my schema on my local machine shows that the full_name column no longer exists in my schema. git is prompting me to check-in this change. I don't want to.

However, I do want to re-introduce the migration that added full_name so that my local and production environments are back in-sync. However, if I were to create a new migration to create full_name, production would ERROR saying that the column already exists. How should I resolve this?


Is it sufficient to create a conditional migration that adds the column so long as it doesn't exist, and to include a down method that drops the column only if it does?

edit/update: I added conditional migrations to remove the bunk columns and tables and all is well.

Jam2 fucked around with this message at 06:58 on Sep 19, 2012

Molten Llama
Sep 20, 2006
Steal the schema.rb from production and db:schema:load that into development. (If for some reason production's schema.rb is missing the column, run a db:schema:dump in production first.)

The existing migrations are then unnecessary, the DB will be up to date with production regardless of what migrations are available, and loading the schema is the correct way to bring up a new instance of the database anyway.

In the future, don't delete migrations, especially if they've been run in production.

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!
I'd add on one thing to what Molten Llama said. Once you have your schema.rb in development set, delete all of your migrations, and create a single new migration with the contents of your schema.rb file in the up method.

He's right that you shouldn't really need the past migrations going forward, but if you're going to leave your migration folder in a state where it technically can't bring a database up to it's current state, you're asking for trouble - what if a developer does try to create a new DB through migrations and something is left in a broken state (and worse yet, you guys are ping-ponging on schema files since he's missing an important column)?

Alternatively, if you don't want to lose your migrations folder, you could have just gone back into git and found the old migration and recreated it - so long as the identifier at the beginning matches, Rails won't re-run the migration (or, if you need to get REALLY fiddly with what you want torun, you can manipulate the contents of the schema_migrations table in your DB)

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Jam2 posted:

I'm having a database issue.

At some point, I introduced a migration to add a full_name string to my user model. This was applied to the production database and now the column exists in the app_production db. Around this time, I had some issues with migrations and had to delete the migration that introduced this column.

See what entry in schema_migrations in production doesn't have a corresponding migration file, and put the new column in a file with that name.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
e: Nevermind. It does work. I have many other issues but this isn't one.

leaving it here anyway


Working on a gem for Google Adwords where their API requires me to use different services and send requests with different field types some required some not etc. So, I started trying to build a structure for the files wherein I could define said fields.

code:
module MyGem
  class Ad < Item
    include MyGem::Deleteable

    field :ad_group_id,       :f, :r, :s
    field :status,            :f, :r, :s, d: "ENABLED", w: %w{ENABLED PAUSED DISABLED}
    field :stats,             :ro
etc.

Where :f means you can filter by it, :r means it is required, :s means you can search by it and on and on. These get parsed and stored as a big list of different class variables for different purposes later when actions are taken on the class such as filtering or searching. So anyway that's what I've been doing.

Running into a snag passing variables between modules in this gem, one of them seems to completely disappear after defining it.

code:
Ad < Item < Api
AdService < Service < Api
Service has a class variable called :item_kinds and when I assign it from Item, it seems to disappear by the time I try and use it from within AdService.

code:
module MyGem
  class Service < Api

    class << self
      def item_kinds; @item_kinds ||= {}; end

      def add_item_kind(symbol, kind_shorthand=nil)
        self.item_kinds.merge!((kind_shorthand || symbol) => symbol)
      end
code:
module MyGem
  class Item < Api

    class << self
      def some_method(symbol)
        # service_class successfully targets the class in the code block above
        # kind_shorthand is just some other guff
        self.service_class.add_item_kind(symbol, self.kind_shorthand)
      end
What ruby gods have I angered later on when I try and use my service's item_kinds method I only get back an empty hash? I have used a logger and I know my some_method is being run with at least a couple of different symbols being passed in there.

I am asking in case this is a ruby thing that people know about.

{}?

Nolgthorn fucked around with this message at 02:44 on Sep 20, 2012

Jam2
Jan 15, 2008

With Energy For Mayhem
Thanks for the tips, guys. Production and development are back in sync.

Lagg
Nov 10, 2005

The wilder the hat, the wilder the man.
We are currently evaluating Rails for a few new projects and while I'm finding answers for most of the detractor's questions, there is something I can't seem to get a good answer for: How to share layouts between multiple applications?

Currently we are using PHP for most of our applications and over a decade of business, a system has emerged so that over 100 applications all use only 5-6 different Smarty templates as what Rails would call "layouts" (of course each application has dozens of Smarty templates, but they are used much more like views)

To pull this off we have some code that does a database lookup based on an ID each application has, and it then just sets the Smarty dir to whatever dir the database returns (I'm summarizing this, it does more, but this isn't a PHP subreddit). All of the developers use the same dev server so we don't have to worry about keeping template repositories up to date or having a bunch of different paths setup (I have issues with this as well, but that's another post)

This system works great if a small layout bug is spotted because it can be fixed on all of the applications at once.

It can be a problem because anytime we want to do something new with layouts, it turns into a huge headache because we have to address every possible case for every possible issue because it maybe used on over 100 applications.

In the research I've done, what little I've found on doing this tells me to use a plugin to pull this off. I wrote a very basic plugin (I have no idea what I'm doing. Most of it was just Google, then copy-and-paste) that uses a Railtie to change the layout dir based on some variable.

This seems to work in the couple of "Hello World" apps I've tested it with, but it would require devs to have a layout repository checked out, and I'm sure each dev would have it pointing at a different location, so each time a dev checked out the project, they would have to change the path again etc.

Furthermore, if there is an update to the plugin, each application would need to update the plugin version, etc. (I only have a rudimentary understanding of plugins, so I'm probably wrong on how this works)
Another way is symlinks, but that just seems like it would cause all sorts of problems, especially when moving between servers/environments.

Some people said use git to import layouts. I see issues with that as well (no auto update when a layout is changed)

This leads me to believe that perhaps sharing layouts between multiple applications is not a good practice, and although it maybe a monumental pain in the neck, going through application by application changing a layout issue might end up being the better way to go in the long run (It would certainly make arbitrary app specific changes easier)

So finally, here are my 2 questions:
1) How can I share layouts amongst applications in a way so that if a layout is updated, all applications that use that layout are updated?

2) Is doing this even a good practice? If not, what are the alternatives?


Let me know if this is long enough to deserve its own thread.

TL;DR - Just read the bold.

Lagg fucked around with this message at 17:21 on Oct 9, 2012

hmm yes
Dec 2, 2000
College Slice
You could share the layout files in a gem. You would still need to bundle update the gem + restart each application when the layout is modified, but that could be automated.

You could also set up git hooks to update the servers automatically when the layouts repo is modified. First time you get a conflict because a dev modified a local layout you're going to be pretty annoyed, though.

Lagg
Nov 10, 2005

The wilder the hat, the wilder the man.

atastypie posted:

You could share the layout files in a gem. You would still need to bundle update the gem + restart each application when the layout is modified, but that could be automated.
...

Could you elborate on this a bit? Would I just need a folder called "layouts" inside of a plugin and then....magic happens? How would you personally do this?

Would I want to make a different gem for each version of the layout we would use?

Could you give details about application restart when changes are made? "Something...Something...Capistrano...MAGIC" right?

I'm barely past the "15 minute blog" stage of Rails.

atastypie posted:

"Git stuff"

I want that to work, but I foresee the same issue you listed,and more.

Thanks for the quick reply.

The Sweetling
May 13, 2005

BOOMSHAKALAKA
Fun Shoe
My dudes, recommend me a good, lightweight blogging platform I can integrate with an existing app. I tried out Toto, but it bugs me that it functions as a separate app and doesn't share my existing templates.

prom candy
Dec 16, 2005

Only I may dance
It'll probably be faster to roll your own than try to find something that fits your needs and integrates nicely with an existing app.

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

So, what's the best approach to enhancing gem-based ActiveRecord extension classes? Specifically I want to add a few class methods to the audited gem.

Suppose I have a "Searchable" module with some helpful class methods that I've successfully integrated into some other ActiveRecord models using ActiveSupport::Concern. It looks kind of like this:

Ruby code:
# app/models/concerns/searchable.rb
# Include this to give your model a searchable pattern
module Searchable
  extend ActiveSupport::Concern

  module ClassMethods
    def search *args*
      # stuff
    end
  end
end
This plays nicely with my user-defined ActiveRecord models but I'm having some trouble opening up the Audited::Adapters::ActiveRecord::Audit class. My current thought process is something like this:

Ruby code:
# lib/audit_extensions.rb
require 'audited'
module AuditExtensions
  def self.included(base)
    # base.send :include, Searchable ??
  end
end
Audit = Audited.audit_class #=> Audited::Adapters::ActiveRecord::Audit
Audit.send(:include, AuditExtensions)
Which doesn't work of course. I can never get past my controller complaining about NoMethodErrors. :(

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?

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
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

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

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

That's not so bad. I'm on a team that writes migrations that look like
Ruby code:
class CreateSuperUser < ActiveRecord::Migration
  def self.up
    add_column :users, :is_superadmin, :boolean
    u = User.new
    u.email = #
    u.name = #
    u.is_male = 1
    u.is_subscribed = false
    u.password = # yes they really do this
    u.password_confirmation = #
    u.status = 1
    u.zip_code = '01128'
    u.is_superadmin = true
    u.save
  end

  def self.down
    remove_column :users, :is_superadmin
  end
end
I wish I were joking.

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe

A MIRACLE posted:

That's not so bad. I'm on a team that writes migrations that look like
Ruby code:
class CreateSuperUser < ActiveRecord::Migration
  def self.up
    add_column :users, :is_superadmin, :boolean
    u = User.new
    u.email = #
    u.name = #
    u.is_male = 1
    u.is_subscribed = false
    u.password = # yes they really do this
    u.password_confirmation = #
    u.status = 1
    u.zip_code = '01128'
    u.is_superadmin = true
    u.save
  end

  def self.down
    remove_column :users, :is_superadmin
  end
end
I wish I were joking.

Notwithstanding that that should be a database 'seed' rather than a migration, don't use models in migrations. Like that, at least.

http://gem-session.com/2010/03/how-to-use-models-in-your-migrations-without-killing-kittens
http://complicated-simplicity.com/2010/05/using-models-in-rails-migrations/

Edit: Also, kill yourself. Then quit.

Molten Llama
Sep 20, 2006
Okay, I've got to know: Is password_confirmation a table column or just innocently satisfying a validation?

Because in my head it's a table column, and I will be mortified/thrilled if it really is.

skidooer
Aug 6, 2001

Molten Llama posted:

Okay, I've got to know: Is password_confirmation a table column or just innocently satisfying a validation?

Assuming you are referring to the field created by validatds_confirmation_of :password, it is just an attr_accessor: https://github.com/rails/rails/blob/master/activemodel/lib/active_model/validations/confirmation.rb#L14

Adbot
ADBOT LOVES YOU

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

It's what happens when you decide to outsource development to a team of Eastern Europeans who don't speak English (our image gallery is spelled "Galery.rb") or care about things like unit test coverage, standards or best practices, and think it's okay to write 500 character SQL queries on a single LOC.

This is for a fairly prominent "daily deals" type site by the way, and the codebase is responsible for processing upwards of 200k in sales / day.

I was brought on last month because the higher-ups were all "oh poo poo we don't have any developers stateside." I'm basically the SQL monkey / put out fires ops guy right now but I'm pushing for a refactor / unit test project before any new features are added. We'll see. At least the pay is good for my area.

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