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
skidooer
Aug 6, 2001

BonzoESC posted:

Golf, anyone?
Ooh, I love golf.

code:
['abc99.6','-100x500','what','w9','e2','e2','e2','-100x501'].sort_by{|a|a[/\d+/].to_i}

Adbot
ADBOT LOVES YOU

skidooer
Aug 6, 2001

Jam2 posted:

How do I add a method at runtime?

This is something that really should be used sparingly, but for the sake of learning:

code:
def method_missing(method, *args, &block)
  if value = method.to_s[/plus(\d+)/, 1]
    plus(value.to_i)
    self
  else
    super(method, *args, &block)
  end
end
And to generate the methods after they have been called so that method_missing is only hit once:

code:
def method_missing(method, *args, &block)
  if value = method.to_s[/plus(\d+)/, 1]
    self.class.send(:class_eval) do
      define_method(method) do
        plus(value.to_i)
        self
      end
    end
    
    send(method, *args, &block)
  else
    super(method, *args, &block)
  end
end

skidooer
Aug 6, 2001

GodIsInTheTrees posted:

I could do it in PHP in 5 minutes but this Rails stuff seems totally overboard and mysterious :(
Rails kind of is overboard for something that simple. What's the justification for using Rails over something like, say, Sinatra or plain old Rack?

skidooer
Aug 6, 2001

enraged_camel posted:

Considering this, why am I getting this error?
The search path will be your current working directory, not the location of the script. If test.txt will always be located along side the script, you can do something like this:
code:
path = File.join(File.dirname(__FILE__), 'test.txt')
File.open(path) { |file| file.each { |line| puts line } }

skidooer
Aug 6, 2001

Plastic Jesus posted:

Then if I want to create a new user in the user controller this will not work:
If we add brackets, your code is equivalent to this:

code:
@user.email_providers.push(EmailProvider.create) do |prov|
   prov.email_address = params[:user][:email_address]
end
As a result, you are not passing any attributes to your EmailProvider model.

Personally, given your input, I'd be inclined to do something like this:
code:
class User < ActiveRecord::Base
  has_many :email_providers
  
  def email_address=(email_address)
    email_providers.build(:email_address => email_address)
  end
end

skidooer fucked around with this message at 23:57 on Jul 12, 2011

skidooer
Aug 6, 2001

Pardot posted:

I want to either congratulate you for knowing to add indicies on the join columns
Or congratulate Rails. It automatically adds indexes on join columns when you create your model. :)

skidooer
Aug 6, 2001

enki42 posted:

Is this new in 3.1 or something?
Oh, maybe. I was thinking it was older than that, but it is present in 3.1 for sure. And yes, you have to tell it that it is a reference column, but that's all.

skidooer
Aug 6, 2001

revmoo posted:

They're still running some Rails, I think on the front-end, but it's a pretty bad example of successful implementations.
Actually, I'd say it's a shining example. Despite the constant Rails hate that came from them and them having rewrote virtually everything else in the stack, they still have not replaced the Rails application itself. If it was really that bad, it would have been the first to go.

skidooer
Aug 6, 2001

rugbert posted:

but how would I set the controller up for the members/images?
You don't have to really do anything special. The routes will provide, in your case, a member_id parameter from the URL (/members/:member_id/images) which you can do stuff with. Otherwise, it is just like writing a controller without nesting.
code:
class ImagesController < ApplicationController
  before_filter :set_member
  
  def index
    @images = @member.images
  end
  
  private
    def set_member
      @member = Member.find(params[:member_id])
    end
end

skidooer
Aug 6, 2001

BonzoESC posted:

It's okay to have multiple controllers that handle the same kind of resource
Though it is also okay to have a single controller handle the same kind of resource in different ways. Personally, unless the way member images are handled is wildly different, I would probably keep everything in the images controller. Of course it is hard to make any recommendations without having knowledge of the entire problem domain.

skidooer
Aug 6, 2001

BonzoESC posted:

I'd rather have multiple (thin) controllers manipulating the same (fat) models than a single controller with a poo poo-ton of conditional logic based on routes manipulating the model.
I don't feel something like this warrants a second controller.

code:
class ImagesController < ApplicationController
  def create
    @image = image_scope.create!(params[:image])
    head :created, :location => image_url(@image)
  end
  
  private
    def image_scope
      if params[:member_id]
        Member.find(params[:member_id]).images
      else
        Image
      end
    end
end

skidooer
Aug 6, 2001

Bob Morales posted:

Memory leak of some sort? Garbage collection??
Ruby retains and reuses its allocated memory, so if you need 200MB of memory at one point in time, the interpreter will allocate that and not release it back to the system. Make sure you're not doing anything like loading thousands of rows into ActiveRecord instances, or reading entire images into memory; stuff like that.

skidooer
Aug 6, 2001

A MIRACLE posted:

code:
DateTime.strptime('#{starts}', '%m/%d/%Y')
DateTime.strptime('#{ends}', '%m/%d/%Y')

In Ruby, single quoted strings are not parsed for interpolated variables. Not sure how necessary your conversion to string is, but keeping with the intent of your code:

code:
DateTime.strptime(starts.to_s, '%m/%d/%Y')
DateTime.strptime(ends.to_s, '%m/%d/%Y')

Adbot
ADBOT LOVES YOU

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

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