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
8ender
Sep 24, 2003

clown is watching you sleep
code:
Player.find[:player_id].leagues << League.find[:league_id]
Does that do what you need?

Adbot
ADBOT LOVES YOU

prom candy
Dec 16, 2005

Only I may dance
Or the other way around if you already have the league

code:
current_league.players << Player.find(params[:player_id])
When you have many-to-many relationships you can interact with the relationships like a normal collection.

Strong Sauce
Jul 2, 2003

You know I am not really your father.





8ender posted:

code:
Player.find[:player_id].leagues << League.find[:league_id]
Does that do what you need?

For the most part, although I changed it as Player.find(params[:player_id]).leagues << League.find(params[:league_id])

This worked, however now I run into another problem where Rails is not pointing to the correct route when I just want to delete the player from the league and not the player itself. Here is my routes table

code:
  delete 'leagues/:league_id/players/:id' => 'players#drop_from_league'
  post 'leagues/:league_id/players/' => 'players#create_league_player'
  resources :players
  resources :leagues do
    resources :players
  end
However when I run a rake routes, it duplicates the 'leagues/:league_id/players/:id' route
code:
---->              DELETE /leagues/:league_id/players/:id(.:format)      players#drop_from_league
                   POST   /leagues/:league_id/players(.:format)          players#create_league_player
           players GET    /players(.:format)                             players#index
                   POST   /players(.:format)                             players#create
        new_player GET    /players/new(.:format)                         players#new
       edit_player GET    /players/:id/edit(.:format)                    players#edit
            player GET    /players/:id(.:format)                         players#show
                   PUT    /players/:id(.:format)                         players#update
                   DELETE /players/:id(.:format)                         players#destroy
    league_players GET    /leagues/:league_id/players(.:format)          players#index
                   POST   /leagues/:league_id/players(.:format)          players#create
 new_league_player GET    /leagues/:league_id/players/new(.:format)      players#new
edit_league_player GET    /leagues/:league_id/players/:id/edit(.:format) players#edit
     league_player GET    /leagues/:league_id/players/:id(.:format)      players#show
                   PUT    /leagues/:league_id/players/:id(.:format)      players#update
---->              DELETE /leagues/:league_id/players/:id(.:format)      players#destroy
           leagues GET    /leagues(.:format)                             leagues#index
                   POST   /leagues(.:format)                             leagues#create
        new_league GET    /leagues/new(.:format)                         leagues#new
       edit_league GET    /leagues/:id/edit(.:format)                    leagues#edit
            league GET    /leagues/:id(.:format)                         leagues#show
                   PUT    /leagues/:id(.:format)                         leagues#update
                   DELETE /leagues/:id(.:format)                         leagues#destroy
              root        /                                              leagues#index
For some reason Rails is not overriding the DELETE /leagues/:league_id/players/:id(.:format), I may end up just creating a leagues_players controller to handle this stuff but does anyone know why Rails isn't overriding the route?

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Strong Sauce posted:

For the most part, although I changed it as Player.find(params[:player_id]).leagues << League.find(params[:league_id])

This worked, however now I run into another problem where Rails is not pointing to the correct route when I just want to delete the player from the league and not the player itself. Here is my routes table

code:
  delete 'leagues/:league_id/players/:id' => 'players#drop_from_league'
  post 'leagues/:league_id/players/' => 'players#create_league_player'
  resources :players
  resources :leagues do
    resources :players
  end

A Player, a League, and a player's Membership (just a suggestion) in a league are different concepts. Being able to name and manage this join relationship as an independent concept is why has_many through: is generally better than has_and_belongs_to_many.

code:
  resources :players
  resources :leagues do
    resources :memberships
  end

Strong Sauce
Jul 2, 2003

You know I am not really your father.





Yeah that's probably going to be the best solution. Still odd that Rails won't override the route though.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Strong Sauce posted:

Yeah that's probably going to be the best solution. Still odd that Rails won't override the route though.

Routes don't "override" until they're evaluated at request time. When you DELETE that route, what action does it route to?

Strong Sauce
Jul 2, 2003

You know I am not really your father.





BonzoESC posted:

Routes don't "override" until they're evaluated at request time. When you DELETE that route, what action does it route to?

When I say override, I meant I specify a different route in routes.rb. When I run rake routes it's suppose to generate what the proper routes should be, and it seems to duplicate the same route. The other route I overrode, "post 'leagues/:league_id/players/' => 'players#create_league_player'" right underneath the DELETE route doesn't show up subsequently down the route path. However the DELETE route still shows up when it's suppose to be overridden.

Right now the route is still using ,"players#destroy" rather than "player#drop_from_league"

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Strong Sauce posted:

When I say override, I meant I specify a different route in routes.rb. When I run rake routes it's suppose to generate what the proper routes should be, and it seems to duplicate the same route. The other route I overrode, "post 'leagues/:league_id/players/' => 'players#create_league_player'" right underneath the DELETE route doesn't show up subsequently down the route path. However the DELETE route still shows up when it's suppose to be overridden.

Right now the route is still using ,"players#destroy" rather than "player#drop_from_league"

Did you try moving your drop_from_league declaration below the resources?

You can also suppress the default delete: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created

Strong Sauce
Jul 2, 2003

You know I am not really your father.





BonzoESC posted:

Did you try moving your drop_from_league declaration below the resources?

You can also suppress the default delete: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created

So moving it below the resources didn't work, but excluding destroy method works! Thanks! Still odd though.

plasticbugs
Dec 13, 2006

Special Batman and Robin

plasticbugs posted:

Regarding Foreman:
I'm not too keen on the lack of color formatting in my terminal output after running rspec. Though I understand that can be fixed if I also install and configure Guard for autotests.

For now, is there any way I can run rspec in pretty colors without Foreman, but still load in my testing environment variables from .env? I'm guessing no.

I answered my own question quoted above in case anyone's interested or it may help someone trying to solve the same problem.

I added the guard-rspec gem to my app and I have a nice autotest setup now. I edited the Guardfile to include color output for rspec per this github comment . However, I could have also just run:
foreman run rspec
and gotten the same result, without installing Guard -- albeit without autotest.

In any event, foreman run your_process_here is a good way to load in your .env variables and have them available to whatever process or rake task you want to run (like Rails console, rspec, etc.). Eg. foreman run rspec

Jam2
Jan 15, 2008

With Energy For Mayhem
Hey, guys. I'm hoping someone here can assist me with a little Model association problem I have.

I have two models:

class User < Act...
end

class Playlist < Act...
end

There is a single user (the user who created it) who maintains executive control of the playlist. This is the owner.

Furthermore, a playlist can be associated with many users (they all share and edit it at the discretion of the "owner") and a user can be associated with many playlists.

Without the owner complexity, I would just use the has_many_and_belongs_to to link the two models.
What's the best way to accomplish these associations?

prom candy
Dec 16, 2005

Only I may dance
Off the top of my head:

code:
class Playlist
  belongs_to :owner, :class_name => "User"
  has_many :playlist_subscriptions
  has_many :subscribers, :through => :playlist_subscriptions, :class_name => "User"
end

class User
  has_many :owned_playlists, :class_name => "Playlist", :foreign_key => :owner_id
  has_many :playlist_subscriptions
  has_many :playlists, :through => :playlist_subscriptions, :class_name => "Playlist"
end
You could also add a role to the playlist_subscriptions table and manage all of the access control that way, but sometimes it's nice to have a canonical owner.

Jam2
Jan 15, 2008

With Energy For Mayhem

prom candy posted:

Off the top of my head:

code:
class Playlist
  belongs_to :owner, :class_name => "User"
  has_many :playlist_subscriptions
  has_many :subscribers, :through => :playlist_subscriptions, :class_name => "User"
end

class User
  has_many :owned_playlists, :class_name => "Playlist", :foreign_key => :owner_id
  has_many :playlist_subscriptions
  has_many :playlists, :through => :playlist_subscriptions, :class_name => "Playlist"
end
You could also add a role to the playlist_subscriptions table and manage all of the access control that way, but sometimes it's nice to have a canonical owner.

Does playlist_subscriptions have to be a model?
Does playlist end up with two has_many associations or is the first just necessary for the second "through" association?

prom candy
Dec 16, 2005

Only I may dance
Check out the documentation on has_many :through, you can see it here under the "Many-to-Many" header: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Jam2 posted:

Does playlist_subscriptions have to be a model?
Does playlist end up with two has_many associations or is the first just necessary for the second "through" association?

You want it to be a model; if it wasn't one, you'd have a hard time testing, describing, and reasoning about the relationship between playlists and users that are subscribers.

prom candy
Dec 16, 2005

Only I may dance
It also makes it easier to add more information about the relationship, if ever you want to get fine grained about permissions, etc.

Oh My Science
Dec 29, 2008
I find myself wanting to Learn more about ruby, has anyone taken the ruby course from pragmatic studio? I could probably grab a book or two, but I'm partial to video tutorials.

abelwingnut
Dec 23, 2002


I began work today realizing that I need to leave. I've got a lot of experience and a few web application ideas and decided to look into Ruby more. After a few hours I ended up finding Poingnant's Guide.

I love this thing, but it seems like it's focused on Ruby. Aside from the 'Hard Way to Code in Ruby', does anyone have any other books/tutorials?

asveepay
Jul 7, 2005
internobody

Oh My Science posted:

I find myself wanting to Learn more about ruby, has anyone taken the ruby course from pragmatic studio? I could probably grab a book or two, but I'm partial to video tutorials.

several of the people on my team have taken this course, and while some of it was remedial for them, even the ones that are coding daily and inside console all the time found it educational.

plasticbugs
Dec 13, 2006

Special Batman and Robin

Abel Wingnut posted:

I began work today realizing that I need to leave. I've got a lot of experience and a few web application ideas and decided to look into Ruby more. After a few hours I ended up finding Poingnant's Guide.

I love this thing, but it seems like it's focused on Ruby. Aside from the 'Hard Way to Code in Ruby', does anyone have any other books/tutorials?

After you get through Learn Ruby the Hard Way, work your way through the Ruby koans to reinforce what you've learned.

Michael Hartl's Ruby on Rails Tutorial was invaluable in helping me put all the pieces together with regards to Rails, testing, best practices and MVC. It's here. The Ruby on Rails Tutorial screencasts are worth every penny, but you can save some money by working your way through the book online for free.

Also, even though they're a little out of date, the Peepcode Rails screencasts are very handy. I got a lot out of the Command Line ones. I recommend it if you're new to the command line. I also enjoyed the Git and Textmate screencasts.

waffle enthusiast
Nov 16, 2007



You might also check out Coursera's SaaS class. It's the free on-line version of UC Berkeley's SaaS course, and covers Ruby, Rails, test-driven development, and a couple of other things over six weeks.

I didn't speak Ruby before I took it and thought it was a pretty good introduction to the language, and rails.

etcetera08
Sep 11, 2008

Not Rails-related, but since we don't have a Ruby thread.. I am having a weird thing where a
Ruby code:
require 'lastfm'
is giving me a "stack level too deep" error. Any ideas what's happening/why? I figure it's something simple I'm too stupid to see.

edit: if I do it in irb it gives me the same error and then says "Maybe IRB bug!!" :confused:

etcetera08 fucked around with this message at 18:58 on Jun 23, 2012

Strong Sauce
Jul 2, 2003

You know I am not really your father.





etcetera08 posted:

Not Rails-related, but since we don't have a Ruby thread.. I am having a weird thing where a
Ruby code:
require 'lastfm'
is giving me a "stack level too deep" error. Any ideas what's happening/why? I figure it's something simple I'm too stupid to see.

edit: if I do it in irb it gives me the same error and then says "Maybe IRB bug!!" :confused:

Which versions of lastfm and dependency gems are you using? Usually when that error occurs it is some kind of infinite recursion problem but that probably won't help you solve this problem.

Oh My Science
Dec 29, 2008

Dangerllama posted:

You might also check out Coursera's SaaS class. It's the free on-line version of UC Berkeley's SaaS course, and covers Ruby, Rails, test-driven development, and a couple of other things over six weeks.

I didn't speak Ruby before I took it and thought it was a pretty good introduction to the language, and rails.

Thanks for this, ill be sure to check it out next. I ponied up for the pragmatic course and although it's not challenging me, I am relearning some basic fundamentals.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

etcetera08 posted:

Not Rails-related, but since we don't have a Ruby thread.. I am having a weird thing where a
Ruby code:
require 'lastfm'
is giving me a "stack level too deep" error. Any ideas what's happening/why? I figure it's something simple I'm too stupid to see.

edit: if I do it in irb it gives me the same error and then says "Maybe IRB bug!!" :confused:

Do you have another file in your load path named lastfm.rb that might be requiring the file with that line?

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.
What is the best choice for a templating system for something like an online choose your own adventure book? The writers would need to be able to access story wide variables. I was thinking something like Mustache.

etcetera08
Sep 11, 2008

Strong Sauce posted:

Which versions of lastfm and dependency gems are you using? Usually when that error occurs it is some kind of infinite recursion problem but that probably won't help you solve this problem.

The Journey Fraternity posted:

Do you have another file in your load path named lastfm.rb that might be requiring the file with that line?

Hmmm, I'm thinking it might be an RVM problem. I'll try to clear everything out and reinstall.

edit: yep, did a gem uninstall and then gem install and it seems to work now. Thanks guys.

rugbert
Mar 26, 2003
yea, fuck you
So I have a quick question regarding hosting. Yes I know about heroku and use it for most of my sites BUT what about high traffic sites?

I made rtattoos.com that really doesnt get any traffic, but if any image is linked to reddit then ill get between 1K and 4K hits a day which isnt much but theyve taken the site down a couple times. Heroku can get kind of expensive so I was wondering if anyone had other suggestions? How does rackspace hold up?

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.
AWS is pretty well priced.

manero
Jan 30, 2006

Sinestro posted:

AWS is pretty well priced.

It totally depends on the use. At some point, a VPS or even a bigass machine colocated is better. If you have high IO requirements, VPS or Amazon might not be the best.

waffle enthusiast
Nov 16, 2007



AWS > Rackspace > Remote colo.

But this very much depends on what you're trying to do. I've been a sysad for a dozen years or so, and these days I'd look to AWS for anything less than half a rack without extremely specific (read: "a metric fuckton" of IOPS) requirements.

Amazon's EC2 and PaaS offerings are miles ahead of anything else out there.

I presume you've looked at Elastic Beanstalk?

waffle enthusiast fucked around with this message at 04:13 on Jun 29, 2012

prom candy
Dec 16, 2005

Only I may dance
Here's a big wide question:

Are you guys doing skinny controller fat model or are you doing skinny controller model as persistence layer and then lots of PORO for your actual business logic? Why/why not?

Lately I've been moving my business logic out of my models and into Plain Old Ruby Objects because they're easier to test and because my models were getting insanely long on big projects. How are you guys handling big projects with Rails?

8ender
Sep 24, 2003

clown is watching you sleep

prom candy posted:

Lately I've been moving my business logic out of my models and into Plain Old Ruby Objects because they're easier to test and because my models were getting insanely long on big projects. How are you guys handling big projects with Rails?

I'm interested to know this because the models in our largish app are just starting to feel a little overweight.

Deus Rex
Mar 5, 2005

8ender posted:

I'm interested to know this because the models in our largish app are just starting to feel a little overweight.

I've never worked on a particularly large Rails project, but I've read about other projects which refactor model behaviors into "concerns" using Ruby modules. Decent example here: http://blog.waxman.me/extending-your-models-in-rails-3

prom candy
Dec 16, 2005

Only I may dance
That's not really refactoring though as much as just splitting up code into smaller files and including them. It's still really helpful when your models get huge, but there's starting to be a lot of people that say that anything not involving the persistence layer doesn't belong in the model.

One example of this is a site that I'm working on right now that needs to build an Invoice out of data that's imported from a Job on another site. I moved all of the logic for importing into an InvoiceImporter class. It makes testing really easy because you can just use a mock for the actual Invoice instance and then make sure the right calls are going to it, and it also maintains Single Responsibility Principle. The class that's responsible for storing and fetching invoices from the database doesn't need to know about the logic behind creating one from a job.

I'm interested in what other people are doing though, I've heard arguments towards continuing to keep a lot of logic in the model and it is sort of The Rails Way.

8ender
Sep 24, 2003

clown is watching you sleep

prom candy posted:

I'm interested in what other people are doing though, I've heard arguments towards continuing to keep a lot of logic in the model and it is sort of The Rails Way.

This is sort of where we're at right now because we've been burned in the past by straying from "the way" when new versions of Rails come out.

App concerns looks pretty good though and having official support in Rails3 is nice.

manero
Jan 30, 2006

prom candy posted:

That's not really refactoring though as much as just splitting up code into smaller files and including them. It's still really helpful when your models get huge, but there's starting to be a lot of people that say that anything not involving the persistence layer doesn't belong in the model.

One example of this is a site that I'm working on right now that needs to build an Invoice out of data that's imported from a Job on another site. I moved all of the logic for importing into an InvoiceImporter class. It makes testing really easy because you can just use a mock for the actual Invoice instance and then make sure the right calls are going to it, and it also maintains Single Responsibility Principle. The class that's responsible for storing and fetching invoices from the database doesn't need to know about the logic behind creating one from a job.

I'm interested in what other people are doing though, I've heard arguments towards continuing to keep a lot of logic in the model and it is sort of The Rails Way.

I've been trying to follow this a bit as well, it certainly slims down the models a bit and improves SRP, makes testing easier, yadda yadda.

Taking it to the extreme, some people will go so far as to only use AR models for persistence and then delegate all business logic to POROs. I haven't played around with this yet, but it seems harder to do in practice.

There was a rubydrama earlier this week (or maybe last) when someone posted a pastie of their rails code after getting canned, and it followed some of these practices, and DHH was chiming in something along the lines of "just use before_filters in your controller" or something, without making a new class or anything.

I'm not sure I agree that you should either put everything in the controller or everything in the model as methods, a little bit of OOP can go a long way in my experience. The trick is to not create a mess in the process.

For some reading material, there's Practical Object Oriented Design in Ruby and also Objects on Rails (free).

I like the O'Reilly book, and Avdi's book is decent but his coding style is a bit strange.

prom candy
Dec 16, 2005

Only I may dance
Avdi's book is what got me thinking this way, and the Destroy All Software screencasts are pushing me further down that road. Sometimes I get the feeling that DHH enjoys his own dog food a little too much but I'm not going to pretend that I know better than him.

Tomed2000
Jun 24, 2002

I'm not sure where to begin with this one but I have a Rails app on Heroku right now that uses Devise for handling user authentication and I've got about 50 customers with accounts. I recently wrote a C# application and I'd like to implement some sort of authentication before users can start running this app. Is there any way I can make the Rails app communicate with the C# app? I know this is possible with Microsoft SQL Server and IIS but I'd really just love to send a request to the postgres database on Heroku for validation or use some other technique that allows me to query the Rails app for user login info.

Tomed2000 fucked around with this message at 04:32 on Jul 6, 2012

Adbot
ADBOT LOVES YOU

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

Tomed2000 posted:

I'm not sure where to begin with this one but I have a Rails app on Heroku right now that uses Devise for handling user authentication and I've got about 50 customers with accounts. I recently wrote a C# application and I'd like to implement some sort of authentication before users can start running this app. Is there any way I can make the Rails app communicate with the C# app? I know this is possible with Microsoft SQL Server and IIS but I'd really just love to send a request to the postgres database on Heroku for validation or use some other technique that allows me to query the Rails app for user login info.

Well, you can take advantage of Rails' RESTful api design. Submit a POST to the corresponding sessions controller (that handles logins) and specify in the headers that you want a json formatted response of some kind. XML would work too I suppose. Then parse the results when they come back from the server in your C# code.
It's really late and I've been coding all day so I might have missed something but that seems like the most straightforward way to do it.

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