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
Big Nubbins
Jun 1, 2004
For those of you that are very familiar with Inherited Resources, I'm having a problem modeling the following association chain in my controllers:

Say I have Users which belong to many Leagues and each user has a single Team for a particular league. The league is identified by a slug as the subdomain, and since there is only a single team for a particular user and league, I don't need to identify that either. So instead of a URL that looks like http://example.com/leagues/1/teams/1, I get http://myleague.example.com/team, which looks a hell of a lot better to me. The problem I ran into with Inherited Resources is describing the relationship between a league (identified by the slug in the subdomain), the logged in user, and a team singleton using its idioms for describing the associations without having to resort to hacky poo poo like before_filter hooks and injecting ID attributes in the params hash. It really seems that something like this would work:
code:
class League::TeamController
    before_filter :authenticate_user!

    defaults :singleton => true
    belongs_to :league, :finder => :find_by_slug, :param => request.subdomain

    def begin_of_association_chain
        current_user
    end

Adbot
ADBOT LOVES YOU

Big Nubbins
Jun 1, 2004

Kim Jong III posted:

Hi Rails people! I'm going to be picking up RoR soon for my job. Does anyone have any recommended tutorials that are focused on someone who has plenty of experience under their belt?

I'll be moving from a mostly Python/Django environment, if that helps. So I'm comfortable with MVC, ORMs, etc.

Sorry if this has been asked before. I tried to look through the thread but it's kinda massive...

If you're going to be using ActiveRecord as your ORM, I highly suggest using Squeel. The AR query methods aren't completely horrible, but get verbose very quickly. If you're doing the "skinny controller, fat model" thing, it'll make the sea of scopes you'll invariably end up with 100 times more readable, and up much less space.

To make your stay at Rails more pleasant, check out these guys' projects on Github to see if there's anything you might want to use:
https://github.com/plataformatec
https://github.com/josevalim
https://github.com/collectiveidea
I personally wouldn't fly without inherited resources, has_scope, and responders again, namely. Coming into Rails, I was initially depressed by how much code repetition there was in controllers.

Big Nubbins
Jun 1, 2004
Apparently I haven't made the effort to understand the semantics of nested vs. scoped resources enough to know whether I'm structuring my routes and controllers wrong. I'm sure it's incredibly basic, but I made some assumptions early on that made things kind of messy. When you have an association like "a blog post has many comments", it's obvious that you'd use a nested resource since comments are always accessed in the context of a post. With the case of something like "a department has many employees", the department context is optional.

I wanted to have "pretty" urls, so at the time I felt I needed 2 controllers: one that's unscoped and one that's always scoped to a department. Obviously you could combine them in one controller, but I didn't want to do something like:
code:
# routes.rb
resources :departments do
    resources :employees
end

# employees_controller.rb
def index
    if params.has_key(:department_id)
        @employees = Employee.find_by_department params[:department_id]
    else
        @employees = Employee.all
end
I don't hate myself that much, and I didn't want to always have scoping bullshit like "department=1" hanging around in the query string. What I ended up with was something like:
code:
# routes.rb
resources :employees
scope 'department' do
    resources :employees
end

# employees_controller.rb
def index
    @employees = Employee.all
end

# department/employees_controller.rb
def index
    @employees = Employee.joins{ department }
        .where{ department.id == (params[:department_id]) } # I love Squeel blocks
end
and the URLs would be something like /employees and /accounting/employees.

This is just a simple example, but the dataset I'm working with is necessarily highly relational and there are much more complex relationships than this. After some time, I started drowning in specialized controllers whose only real difference was in what table(s) they're performing a join to (or not). So where's the middle ground between flexible, "pretty" routing with lots of specialized controllers and a single, swiss-army-controller that handles all your scoping and filtering needs?

Writing this post makes me feel like I'm really bad at Rails.


Thoom posted:

Disregarding my brain fart about scoping rules, I still think it's dumb how poorly documented the scoping of various namespaces is in Rails.
http://www.ruby-doc.org/docs/ (home of the official core API docs as far as I'm aware) reference many guides with detailed info on lexical scoping.

Big Nubbins fucked around with this message at 23:30 on Apr 5, 2012

Big Nubbins
Jun 1, 2004
I think I found the "middle ground" I was seeking earlier in the form of the has_scope gem. Still, I'd like to know what "The Rails Way" is to keep that poo poo DRY.

Speaking of scopes, is there a gem that evals your models to provide basic sorting, filtering, and association scopes? Hobo's Scopes module is pretty much right on the money, but without the reliance on method_missing to work the magic for you. The API I'm putting together is pretty heavy on the sorting and filtering, and it's getting to the point where the majority of my model class definition consists of scope definitions.

Big Nubbins
Jun 1, 2004
I think I did a bad thing. This morning, mysteriously my rails server wouldn't start. It was bombing out with the message /Users/ShameBoner/.rvm/gems/ruby-1.9.3-p286@rails_3_2/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- trace_nums (LoadError). I suspect that I properly hosed my gemset by doing a bundle update while rubygems.org's dependency API is down. I'm not sure what gem is responsible for the TraceNums class, but it seems to be missing. Is there any way to un-gently caress this gemset? For the time being, I had to go back to using my 1.9.2 version of Ruby.

Big Nubbins
Jun 1, 2004

A MIRACLE posted:

Wow, that seems kind of awkward, I didn't know people still inlined ruby in their javascript code. I hate to recommend js frameworks since it seems like you're already really far along with this method, but I've been really happy using either Angular, Backbone, or Knockout.js (with underscore.js as a utility belt) for my ajax-y goodness.

I'd also give Spine.js a look as an alternative to Backbone. Spine is written in Coffeescript, has no dependencies, and is teeny tiny. After learning Coffeescript, I found that it made me love Javascript more, but also never want to write Javascript again, if you know what I mean. There are some ugly parts of the syntax (mixed array/hash objects, I'm looking at you), but I find it much more readable, concise, and faster to write. Unlike Backbone, Spine doesn't really have a concept of a view, so you must plug your own templating solution into it. I use HAML/Coffeescript through the awesome haml_coffee_assets gem.

Big Nubbins
Jun 1, 2004
In ActiveRecord, Is there a good way to coalesce two (in this case) associations with the same model to form another, cleanly? If I lost you, here's what I'm trying to do:

I have a Transaction model that's acting as a sort of audit log for transactions of virtual currency within the software. The Transaction model has two polymorphic attributes: receivable and payable. Think of a receivable or payable as "an entity that can pay or receive currency". In this case, my User model has two associations with Transaction: credits where the User is a receivable and debits where the User is a payable. What I want to do is have a third association, transactions that coalesces credits and debits. I want it to be an association and not simply a method on User so I can chain and scope it.

I'm having a REALLY hard time writing an association definition that should read like "has many :transactions where User is either a receivable or a payable".

Big Nubbins
Jun 1, 2004
Is there a way to set the version of SSL used when you set "config.force_ssl" to true in Rails 3.2? I'm forcing SSL in my configuration and having an issue with requests to our payment gateway timing out. For what it's worth, we're using ActiveMerchant.

We managed to reproduce the issue in the following script:
code:
#! /usr/bin/env ruby
require 'net/https'
require 'uri'

uri = URI.parse(ARGV[0] || 'https://secure.networkmerchants.com/gateway/transact.dll')
http = Net::HTTP.new(uri.host, uri.port)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true if uri.scheme == "https"  # enable SSL/TLS
http.ssl_version = :SSLv3
http.start {
  http.request_get(uri.path) { |res|
    print res.body
  }
}
Commenting out the "http.ssl_version = :SSLv3" line causes the request to time out.

Edit: Never mind, I found out that I can simply monkey-patch the SSL version into ActiveMerchant::Connection's "configure_ssl" method.

Big Nubbins fucked around with this message at 17:38 on Dec 20, 2012

Adbot
ADBOT LOVES YOU

Big Nubbins
Jun 1, 2004

Smol posted:

Hope you're not using OpenSSL::SSL::VERIFY_NONE in the real app.

God no, that was just a sample script set up for testing. I ended up fixing it by forking ActiveUtils (ActiveMerchant dependency) and setting the SSL version in the same place it's setting the rest of the SSL options.

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