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
drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

atastypie posted:

Hobo is brand new, so there is very little up to date documentation let alone an entire book. You could print off the docs from the website and reference those instead.

I have a problem that I am sure is actually really simple: I want to add up and return a total price across a m2m association. Using the generic example, I want to be able to add up the total price of all Books based on an Author.

Author
habtm :books

Book
habtm :authors
t.column "price", :integer

Can anyone help me out?

Out of curiosity, why are you using habtm instead of has_many in this situation? This looks like an instance where an author should have many books and a book belong to an author. Your domain may be a bit different though. Anyway, you should just be able to do the following:

author = Author.find(:first)
price = author.books.sum(:price)

However, you would want to do something like this:

Models:
code:
class Author < ActiveRecord::Base
  has_many :books do
    def all_books(args)
      find(:all, args)
    end

    def total_price(reload=false)
      @total_price = nil if reload
      @total_price ||= sum(:price)
    end
  end
end

class Book < ActiveRecord::Base
  belongs_to :author
end
Controller (figuring you're doing a list of some sort)
code:
before_filter :find_author
private
  def find_author
    @author = Author.find(params[:author_id])
  end

public
  def index
    @books = @author.books.all_books
    @total_price = @author.books.total_price
  end
Your views you would just reference the @books and @total_price objects. This also assumes that you would be looking at your books based on the route of /authors/:author_id/books.

Not sure what reasons you have for making price an integer, but it could be a decimal column as well. See http://pragdave.pragprog.com/pragdave/2006/07/decimal_support.html.

Adbot
ADBOT LOVES YOU

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

shopvac4christ posted:

What I'm trying to say is, why can't we have something along the lines of a deployments directory, that has deploy.rb's and subdirectories for all the applications we can deploy, which is completely unrelated to any local checked out copies of the source?

For example,
code:
# ls ~/Deployments
MyApp1 MyApp2 MyApp3
# ls ~/Deployments/MyApp1
deploy.rb
Since the SVN information is in the deploy.rb, there's no need to even have a checked out copy of the application available to deploy it. It doesn't even have to be on a server or anything - just a machine that has deployment recipes that may or may not also be used for development.

This is true and you don't technically need to follow the way capistrano does things. It would really help if you did, though, because cap is opinionated (but configurable) software.

Given your example, I'm not sure I see the benefit in having ~/Deployments/MyApp1/deploy.rb vs ~/Apps/MyApp1/config/deploy.rb. You've traded having a deployments directory for having a directory with access to the source code. Also, you may want to have custom recipes which may be easier to remember why the functions do what they do if they're right there in the app.

Also, since is the RoR love in thread, I just started a RoR news podcast. Shameless self promotion at http://www.railsenvy.com/podcast.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

Anal Wink posted:

I've used acts_as_solr before. What's the difference between ferret ad acts_as_solr? They both use lucene if I'm not mistaken.

Ferret is lucene written in ruby, solr is written in Java. I'd say to stick with solr or sphinx for full text search. Funny story about that. I was at the Rails Edge conference sitting next to Ezra and asked him what he thought of Ferret. He said it was great for development but once you get to more traffic it tends to buckle. Fifteen minutes later I get an emergency "our site is down" email from the clients for whom I just deployed a ferret based solution. I've heard a bunch of reports about people deploying ferret, though, so take that with a grain of salt.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

savetheclocktower posted:

Seconding Slicehost. I just moved from shared hosting to a 256MB slice and the process was ridiculously smooth. A VPS has more of a learning curve than shared hosting, since you're now in charge of everything, but I credit Slicehost's exhaustive library of HOWTOs for easing that transition. I don't run Rails apps on mine, but many others in the Slicehost community do, and they appear to love it.

Thirding Slicehost. They are the best for Rails apps unless you go with Rails Machine or Engineyard, which are both *extremely* pricey. Do not go with Media Temple for a Rails app -- you will hate life. I've had poor experience with their dv for a client and a friend of mine is in the process of moving his gs to a different host, probably slicehost.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

Nolgthorn posted:

Help!

I have a technical interview coming up for employment in a salary based role using Ruby on Rails. Up until now all of my professional work with Rails has been under contract and I've done or learned about only what I wanted to know.

I'm really nervous because I realize that -first of all- I want the job, but that I don't know dilly about what is expected when working with other programmers.

That's not true, I learned some about Microsoft SVN while I was getting my MCAD in school, but MCADs don't help in Rails!

Should I know about SVN, Mongrel, page caching and stuff because I don't know about those things. I've always deployed by hand to whatever was available on the server, I've created about 20 commercial web sites in the last year.

Is there anything else I should quickly learn about in the next day or so that wouldn't have been covered in all the blogs out there?
    If it were me, I'd read up on the following:
  • You will need to know SVN stuff at least. As another poster said, though, it's very easy to pick up.
  • Read everything on The Rails Way
  • Know what presenters are and when to use them.
  • Skinny controllers, fat models.
  • Become familiar with the different AR relationships and their uses (include polymorphic if you have the time).
  • You should have an idea of what Mongrel is and know that apache/nginx proxy out to your mongrel instances.
  • Be familiar with migrations.
  • Bonus: Memcache (cache-fu) and file uploads (attachment_fu).

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

Plastic Jesus posted:

Is there an easy way to find out in irb which method is faster in these situations? I'd like to know that the performance difference is for regex v. unpack v. anything else. And while we're on the subject, does Ruby use the old-style (and much faster) Bell Labs DFA regex algorithm or the weird (and slower) perl-style algorithm?

code:
require 'benchmark'

str = "ENTRY9_blahblah"
repeating = 50000

Benchmark.bm(19) do |t|
  t.report('regex: /ENTRY(\d+)/') do
    repeating.times { (/ENTRY(\d+)/.match(str))[1].to_i }
  end

  t.report('regex: /ENTRY(\d?)/') do
    repeating.times { (/ENTRY(\d?)/.match(str))[1].to_i }
  end

  t.report('regex: /Y(\d{1,})/') do
    repeating.times { (/Y(\d{1,})/.match(str))[1].to_i }
  end

  t.report('unpack:') do
    repeating.times { str.unpack("xxxxxa").to_s.to_i }
  end
end
Gives me this:

code:
                         user     system      total        real
regex: /ENTRY(\d+)/  0.140000   0.000000   0.140000 (  0.141113)
regex: /ENTRY(\d?)/  0.130000   0.000000   0.130000 (  0.137405)
regex: /Y(\d{1,})/   0.140000   0.000000   0.140000 (  0.142925)
unpack:              0.110000   0.000000   0.110000 (  0.105741)
So unpack might be the fastest, but marginally. I'd use the regex for readability.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

atastypie posted:

Jumping in to say that if you haven't tried out Passenger/mod_rails... give it a go. It's pretty awesome: just create your vhost file for the application and everything works from there. No setting up mongrel clusters, no extra configuration, easily stop/start, no PID problems. Pretty straightforward to setup cap tasks of your own to do things like restart the application or disable a vhost.

Just wanted to +1 that. Also if you're on OS X check out the Passenger pref pane: http://www.fngtps.com/2008/06/putting-the-pane-back-into-deployment to add it right in to system preferences.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back
Bump because Rails 2.2 rc1 is out:
http://weblog.rubyonrails.org/2008/10/24/rails-2-2-rc1-i18n-thread-safety-docs-etag-last-modified

Improvements include:
* Thread safety
* ActiveRecord connection pool
* Mailer layouts
* Ruby 1.9 compatibility
* Enhanced etag support
* Internationalization
* Full list of release notes

To install:
code:
gem install rails -s [url]http://gems.rubyonrails.org[/url] -v 2.2.0
This update will possibly break parts of your existing apps.

And a bit of shameless self promotion if you want a pdf/video of what's new:
http://envycasts.com/products/ruby-on-rails-22-package-deal

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

Hammertime posted:

Is this moving in the direction of parallel database access?

Well, the connection pool now allows you to do concurrent connections assuming you have a non-blocking database driver such as neverblock. Pratik from the core team put up a good blog post explaining everything:
http://m.onkey.org/2008/10/23/thread-safety-for-your-rails

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

jonnii posted:

I think I found about it from the rails envy podcast. Now I answer all gem related questions on stackoverflow with RVM propaganda in the hope that it gets more traction.

I found out about it from the internet. But seriously I found out about it from http://delicious.com/popular/ruby -- I have it set up in RSS menu.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

Nolgthorn posted:

This is because the name field is used to render the model object as a parameter in my model. But I don't really know how to fix it. The best thing would be if I could pick and choose times that to_param renders as the name, or renders as a id.

It doesn't feel too railsy but I've done that in the past by doing page_path(@page.id) for occasions where it needs the id and letting to_param take over in the instances where it doesn't.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back
Not a joke: Would any goons be interested in helping create a blog engine using rails 3 with a mongodb backend? I hesitate to say that the world needs another blogging engine but rails sure does. I really don't like any of the existing ones.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back
I wanted to post this because I figured it out the other day and it's pretty cool. I've been using rvm for development lately and different gemsets for each project. It gets to be cumbersome to switch gemsets and versions each time you go in to a project. Rvm supports config files in the form of .rvmrc per project and can be set to automatically change ruby versions. The syntax is as follows:

code:
rvm version@gem_set_name
Combine that with Tammer Saleh's rvm in bash prompt instructions and it's been pretty useful.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back
Does anyone have any tricks for sanitizing and parsing international phone numbers that may be in no particular format? I've checked GitHub and Rubygems for libraries and the best one seems to be http://github.com/carr/phone, however, it still chokes with a lot of my data. Any recommendations that don't have me writing tons of custom regexe[s|n]?

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

jonnii posted:

I use high_voltage (http://github.com/thoughtbot/high_voltage) for my static pages.

+1 for this. High voltage is awesome.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back
To everyone asking about servers and managed hosting, I highly recommend moonshine: http://github.com/railsmachine/moonshine for managing your own servers. It's really easy to install and configure everything and if you're using a vps you can always roll back pretty easily.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

rugbert posted:

I was taught to use type so that I could have inherited models. So I have a PageContent model and then I have a BlogPost model inheriting from it and I needed to have they type field to make it work. Which is does, but in this case I want to reject all BlogPost entries.

Also Im using rails 3

Try doing the equivalent query that Hammertime suggested but with rails 3 syntax:

code:
@pages = PageContent.where('type != ?', 'BlogPost').all
As a side note, if you did want to use reject here, you'd have to use the class or cast it to a string because it's inherited:

code:
@pages = PageContent.all.reject{|p| p.class.to_s == 'BlogPost'}
I'd recommend the first one, though, so you're not doing extra work.

Adbot
ADBOT LOVES YOU

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

rugbert posted:

Oh cool thanks. That did it.


So about heroku, Ive noticed that when Im using s3 and paperclip to upload images everytime I push new changes all of my uploaded images break. Is there a specific way I should set up my model? Right now I have:"

code:
:url => "/:attachment/:id/:style/:basename.:extension",
:path => "/:rails_root/public/:attachment/:id/:style/:basename.:extension"

You're telling paperclip to use the options for local storage there so it's probably not getting symlinked on each deploy (which you don't want to use with Heroku anyway). Have a look at the rdoc for the s3 storage and adjust the parameters accordingly: http://rdoc.info/github/thoughtbot/paperclip/master/Paperclip/Storage/S3.

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