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
Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I've been toying around in Rails 4 using the screencast from Ryan Bates (that stuff is good).

Rails 4 is using minitest, which is awesome. But by default when you create a new model tests in Rails are being wrapped in TestCase which I guess is designed to make minitest look and act like test unit. Is there any way to tell Rails "no I want minitest I don't need your ridiculous extra pointless layers of complexity". Or do I just hack at the tests to make them use minitest and minitest::spec myself?

Adbot
ADBOT LOVES YOU

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I too, would suggest a date field and a second field that describes the accuracy of the date. Then you could construct a method that puts the two together.

released_at: 1-1-2013
period: 'quarter'

looks like: Q1 2013

Or something like that. I'd imagine it's best to have a date column any way you decide to do it, just for sort order.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Nolgthorn posted:

I've been toying around in Rails 4 using the screencast from Ryan Bates (that stuff is good).

Rails 4 is using minitest, which is awesome. But by default when you create a new model tests in Rails are being wrapped in TestCase which I guess is designed to make minitest look and act like test unit. Is there any way to tell Rails "no I want minitest I don't need your ridiculous extra pointless layers of complexity". Or do I just hack at the tests to make them use minitest and minitest::spec myself?

Answering my own question from a few pages ago in case it is helpful to someone else. Default support for Spec was one of the many things taken out of Rails 4 in order to expedite initial release so to get it working use this unofficial gem.

https://github.com/metaskills/minitest-spec-rails

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I learned how to use Ruby and Rails simultaneously. I kind of regret that now and wish I'd learned Ruby first. It's a spectacular language but often when I use it I mix up what is just Ruby and what is Rails.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I've got a Cool question for ActiveRecord peeps.

Take a look.

Activerecord exposes a very helpful .build method on associations.
Ruby code:
# In associated model
def build_a_thing(name)
  things.build(name: name)
end

def another_way_to_build_a_thing(name)
  build_thing(name: name)
end
A useful but separate thing you can do is essentially define a scope using a class method.
Ruby code:
# class Thing
scope :recent, -> { order(created_at: :desc) }

def self.only_nils
  where(name: nil)
end

Thing.recent.only_nils
ActiveRecord appears to have no understanding about what I am trying to do when I combine them.
Ruby code:
# class Thing
def self.find_or_build_from_full_name(full_name)
  name = full_name.split(' ').first
  find_by(name: name) || build(name: name)
end
It breaks on build.

quote:

Undefined method 'build' for Class.

So if I can do:
Ruby code:
# class Thing
def self.find_from_full_name(full_name)
  name = full_name.split(' ').first
  find_by(name: name)
end

things.find_from_full_name(full_name)
In place of:
Ruby code:
# In associated model
name = full_name.split(' ').first
things.find_by(name: name)
Why can't I do:
Ruby code:
# class Thing
def self.build_from_full_name(full_name)
  name = full_name.split(' ').first
  build(name: name)
end

things.build_from_full_name(full_name)
In place of:
Ruby code:
# In associated model
name = full_name.split(' ').first
things.build(name: name)
?

Or even better, how would I do this.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Yeah.

My confusion comes from associations and classes both having find_by but not both having build and therefore it strictly not being available. In my example should I assume the find_by isn't doing what I want in this instance:

Ruby code:
# class Thing
scope :no_cats, -> { where(cats_count: 0) }

def self.find_from_full_name(full_name)
  name = full_name.split(' ').first
  find_by(name: name)
end

# 1
Thing.no_cats.find_from_full_name(full_name)

# 2
name = full_name.split(' ').first
Thing.no_cats.find_by(name: name)
Will #1 return a record that has cats and #2 will not, do they both behave the same?

Nolgthorn fucked around with this message at 17:00 on Mar 14, 2013

Adbot
ADBOT LOVES YOU

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Pardot posted:

timestamptz normalizes and only actually stores it as utc. It takes care of everything.

If timestamp and timestamptz both store the value in utc then how is Rails wrong? Isn't Rails handling timezones itself, independent from any database?

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