- 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}
|
#
¿
Jun 13, 2011 20:31
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 14, 2024 05:49
|
|
- 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
|
#
¿
Jun 17, 2011 18:18
|
|
- 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 } }
|
#
¿
Jul 4, 2011 05:31
|
|
- 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
|
#
¿
Jul 12, 2011 23:54
|
|
- 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.
|
#
¿
Jul 14, 2011 16:18
|
|
- 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.
|
#
¿
Jul 31, 2011 06:52
|
|
- 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
|
#
¿
Aug 11, 2011 21:59
|
|
- 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.
|
#
¿
Aug 12, 2011 07:37
|
|
- 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
|
#
¿
Aug 12, 2011 19:22
|
|
- 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.
|
#
¿
Sep 22, 2011 18:31
|
|
- 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')
|
#
¿
Sep 29, 2011 21:15
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 14, 2024 05:49
|
|
- skidooer
- Aug 6, 2001
-
|
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
|
#
¿
Oct 17, 2012 01:26
|
|