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
raej
Sep 25, 2003

"Being drunk is the worst feeling of all. Except for all those other feelings."

prom candy posted:

Are you sure your cellar method is saving the user_id? Try rewriting your code like this. Note that I got rid of that nasty condition for you :)

code:
def cellar!(beer, year, size, qty)
    cb = cellared_beers.where(:beer_id=>beer.id, :year=>year, :size=>size).first_or_initialize(:user => self)
    cb.qty = cb.qty.to_i + qty
    cb.save
  end
As far as calling cellar! from a form, I'm not sure what exactly you're trying to do. You're basically asking how to build a front-end for this functionality? At that point it might be useful to ask if this code even really belongs in the User model at all. Should the class that's concerned with persisting and authorizing a user really also be responsible for cellaring beers? Maybe the whole thing belongs in a new class.

cellar! is invoked from a user object. Namely current_user.cellar! The idea would be to have a link on a beer page when a user is signed in. This will give user (signed in user) and beer (beer page viewing), and the form will give the option of year, size, and quantity.

Adbot
ADBOT LOVES YOU

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
I'd just like to say that that method name really shouldn't have a bang on it.

Smol fucked around with this message at 21:25 on Jun 11, 2013

prom candy
Dec 16, 2005

Only I may dance
I tend to add bangs to methods that write to the database, which probably isn't correct.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
It doesn't help that there are multiple definitions. Most of the Ruby core/standard library treats it as an indicator that the method will not return anything and will modify its calling object directly. Rails tends to use it to designate that failure should raise an exception (at least in ActiveModel's viewpoint).

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
Bangs should be used when you have two variants of some operation, one "dangerous" and the other "safe". In the most common case, the dangerous variant modifies the receiver directly and the safe on returns a new copy (example: String#gsub! and String#gsub). In ActiveRecord operations, the dangerous variant may raise an exception while the safe will not (e.g. ActiveRecord::Base.save! and ActiveRecord::Base.save).

prom candy
Dec 16, 2005

Only I may dance
Is there a good way to indicate that a method will not just change an instance's attribute but also save it?

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

prom candy posted:

Is there a good way to indicate that a method will not just change an instance's attribute but also save it?

I use the bang for that.

prom candy
Dec 16, 2005

Only I may dance

Cocoa Crispies posted:

I use the bang for that.

So do I but now I'm being told that's wrong.

KoRMaK
Jul 31, 2012



How would I dynamically add an after filter based on the conditional of whether an instance method exists?

I am making a module that attachs to controllers and I want to add an after filter to a method only If said method exists

manero
Jan 30, 2006

KoRMaK posted:

How would I dynamically add an after filter based on the conditional of whether an instance method exists?

I am making a module that attachs to controllers and I want to add an after filter to a method only If said method exists

Maybe something like:

code:
if controller.respond_to?(:a_method_name)
  controller.class.send(:after_filter, :the_new_filter_name)
end
Totally untested, but that should get you in the right direction.

KoRMaK
Jul 31, 2012



I would think that should work, but the class doesn't have the method in its class methods at te time the module gets run. So it's like the method doesn't exist for me to put a filter on.

KoRMaK fucked around with this message at 19:22 on Jun 12, 2013

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

prom candy posted:

So do I but now I'm being told that's wrong.

If nobody actually working on the project has a problem with it then use it. When I have model methods that make changes like that, I give 'em an exclamation point to denote that they're exciting.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

KoRMaK posted:

How would I dynamically add an after filter based on the conditional of whether an instance method exists?

I am making a module that attachs to controllers and I want to add an after filter to a method only If said method exists

Why?

KoRMaK
Jul 31, 2012



Why not?
It will be better this way than editing 30 files to conform.

KoRMaK fucked around with this message at 19:37 on Jun 12, 2013

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

KoRMaK posted:

Why not?

Because it's turning out to be difficult?

You might see if making the Module into an ActiveSupport::Concern would help, or you might describe what the end goal is (see https://en.wikipedia.org/wiki/5_Whys ) so we can figure out if there's a more generally accepted way to do what you want.

KoRMaK
Jul 31, 2012



C'mon Don't logic some sense into me.

I sill think it be useful to add filters from a module.

Wait, adding a filter on that function inside the controller doesn't work either. I should go read up on filters.

E: wtf filters only get run on the methods that are called during routing? I added my before filter to edit, instea of a function that gets called by edit, and it started to work. I guess I had a fundamental misunderstanding of when filters are run.

KoRMaK fucked around with this message at 23:06 on Jun 12, 2013

it is
Aug 19, 2011

by Smythe
I wanna make rubygems use ruby 2.0.0

Here's the output to gem env:
code:
RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.25
  - RUBY VERSION: 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /usr/bin/ruby1.8
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/lib/ruby/gems/1.8
     - /home/matt/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gem" => "--no-rdoc --no-ri"
  - REMOTE SOURCES:
     - [url]http://rubygems.org/[/url]
I've spent like an hour on google trying to figure out exactly how to set the RUBY VERSION to 2.0.0. Can someone just give me the answer because there is no way it's that hard to say "you know that incorrect ruby version? make it something else" but I am having a hell of a time figuring out how to do so. Thanks guys :h:

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
You're using Ruby 1.8 provided by your operating system. Install ruby 2.0 with rbenv & ruby-build or rvm or similar.

prom candy
Dec 16, 2005

Only I may dance

raej posted:

cellar! is invoked from a user object. Namely current_user.cellar! The idea would be to have a link on a beer page when a user is signed in. This will give user (signed in user) and beer (beer page viewing), and the form will give the option of year, size, and quantity.

Right, what I'm saying is maybe it shouldn't be. Maybe the user class shouldn't be responsible for this particular action. http://en.wikipedia.org/wiki/Single_responsibility_principle

raej
Sep 25, 2003

"Being drunk is the worst feeling of all. Except for all those other feelings."

prom candy posted:

Right, what I'm saying is maybe it shouldn't be. Maybe the user class shouldn't be responsible for this particular action. http://en.wikipedia.org/wiki/Single_responsibility_principle

Maybe I need to step back and get an idea of the Ruby way to accomplish the task I want to accomplish.

Users have many beers through cellared_beers (A person can cellar many different kinds of beers)
Beers have many users through cellared_beers (A particular beer can be cellared by many different users)
Cellared_beers has extra attributes that describe the physical bottle in a user's cellar (year, size)

A Beer page displays info on a particular beer (Brewery, description, ABV, etc).
If a person is logged in, there should be a button to add the beer to the current_user's cellar.
This will ask for year and size, as well as quantity.
If a cellared_beer matches for the current_user for that beer with the same year and size, add quantity to that cellared_beer's existing quantity (Bought a 6 pack of 2013 12oz bottles, then bought another 6-pack of 2013 12oz bottles)
If cellared_beer does not match on all three attributes, create cellared_beer with that quantity.

I think I am trying to over complicate this.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

raej posted:

Maybe I need to step back and get an idea of the Ruby way to accomplish the task I want to accomplish.

Users have many beers through cellared_beers (A person can cellar many different kinds of beers)
Beers have many users through cellared_beers (A particular beer can be cellared by many different users)
Cellared_beers has extra attributes that describe the physical bottle in a user's cellar (year, size)

A Beer page displays info on a particular beer (Brewery, description, ABV, etc).
If a person is logged in, there should be a button to add the beer to the current_user's cellar.
This will ask for year and size, as well as quantity.
If a cellared_beer matches for the current_user for that beer with the same year and size, add quantity to that cellared_beer's existing quantity (Bought a 6 pack of 2013 12oz bottles, then bought another 6-pack of 2013 12oz bottles)
If cellared_beer does not match on all three attributes, create cellared_beer with that quantity.

I think I am trying to over complicate this.

What they're trying to say is that cellar! is not a particularly good method for a User to have. If it were me, I'd be doing something like this:

Ruby code:
class CellaredBeer < ActiveRecord::Base
  def self.add_to_cellar(user_id, beer_id, size, quantity)
    cb = where(user_id: user_id, beer_id: beer_id, size: size).first_or_initialize(quantity: 0)
    cb.quantity += quantity
    cb.save!
    cb
  end 
end
Then call that in your controller method. Or a service object, whatever floats your boat.

This is completely off-the-cuff and I may have had a few beers.

The Journey Fraternity fucked around with this message at 05:04 on Jun 13, 2013

Pardot
Jul 25, 2001




The Journey Fraternity posted:

This is completely off-the-cuff and I may have had a few beers.

Where did you get the beer from?

KoRMaK
Jul 31, 2012



Is there an editor or eclipse plugin that will open the other 2 files of a ror object when I open 1?

I want to open the beer form view and have the model and controller open in editor windows for it automatically.

I am now in the realm of shaving off repetitive tasks that take seconds because of their cost over a career-time. And I am lazy. I mean efficient.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

KoRMaK posted:

Is there an editor or eclipse plugin that will open the other 2 files of a ror object when I open 1?

I want to open the beer form view and have the model and controller open in editor windows for it automatically.

I am now in the realm of shaving off repetitive tasks that take seconds because of their cost over a career-time. And I am lazy. I mean efficient.

It's not "ror" unless you're a recruiter, it's "Rails." Objects only usually have one file. Just because the scaffold generator makes files with similar names doesn't mean they have to be related.

Don't bother microoptimizing your workflow when you barely know what's going on; it'll save seconds but cost hours.

raej
Sep 25, 2003

"Being drunk is the worst feeling of all. Except for all those other feelings."

The Journey Fraternity posted:

What they're trying to say is that cellar! is not a particularly good method for a User to have. If it were me, I'd be doing something like this:

Ruby code:
class CellaredBeer < ActiveRecord::Base
  def self.add_to_cellar(user_id, beer_id, size, quantity)
    cb = where(user_id: user_id, beer_id: beer_id, size: size).first_or_initialize(quantity: 0)
    cb.quantity += quantity
    cb.save!
    cb
  end 
end
Then call that in your controller method. Or a service object, whatever floats your boat.

This is completely off-the-cuff and I may have had a few beers.

So on the beer view, have a form_for add_to_cellar and pass in current_user.id and beer.id (from the page), and size, year, quantity from the form?

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
Pass beer.id, size, year and quantity to a form. Grab the user id from the session.

KoRMaK
Jul 31, 2012



Cocoa Crispies posted:

It's not "ror" unless you're a recruiter, it's "Rails." Objects only usually have one file. Just because the scaffold generator makes files with similar names doesn't mean they have to be related.

Don't bother microoptimizing your workflow when you barely know what's going on; it'll save seconds but cost hours.
What is this? I use short hand to represent my ideas and I get a reply that implies I don't know what's going on?

Rails object in my post meant a rails scaffolded set of views a controller and a model centered around one name working in conjunction with each other.

I'd still want the option for the model and controller to open up when I open a view when appropriate.

KoRMaK fucked around with this message at 18:02 on Jun 13, 2013

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
Split views and an IDE like Rubymine (highly recommended, it's well worth the price) allow you to do that easily. Alternatively, you could use something like rails.vim for vim or rinari for emacs.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Cocoa Crispies posted:

It's not "ror" unless you're a recruiter, it's "Rails." Objects only usually have one file. Just because the scaffold generator makes files with similar names doesn't mean they have to be related.

Don't bother microoptimizing your workflow when you barely know what's going on; it'll save seconds but cost hours.

For me, it's definitely the largest minor annoyance in working with rails, and it's a totally legitimate thing to ask about. Rails' file structure, while logical, is pretty frustrating to navigate. Usually by the time I scroll through my views, I've forgotten what I'm looking for.

quote:

Just because the scaffold generator makes files with similar names doesn't mean they have to be related.

This is absurd.I didn't use the scaffold generator when I was starting out, but I still gave things similar names and put them in similar folders and obeyed the MVC, because it literally wouldn't work otherwise. Rails is built around the MVC and it places a huge emphasis on convention. If you give your controllers and models different names, you're going to make things hard on yourself. If you put files in the wrong folder, you're going to make it hard on yourself. Just because you can get around the convention, that doesn't you don't "get" ror unless you put everything in non standard locations. It's called Ruby on Rails for chrissake.

That said, I don't have an answer to the question. I tried creating a folder of symlinks outside my rails directory, and those folders grouped related models/controllers/views. But it was too much of a hassle to deal with.

prom candy
Dec 16, 2005

Only I may dance
Sublime Text 2 along with Cmd/Ctrl-T file searching and a couple plugins for quickly creating new files and folders is a pretty good environment.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

Smol posted:

an IDE like Rubymine (highly recommended, it's well worth the price)

Counterpoint: If you type fast and/or are used to the responsiveness of editors like Sublime Text, Rubymine will feel like you are using VNC or something. This kills my flow, but YMMV.

kitten smoothie
Dec 29, 2001

If Your Business Uses Rails 2.3 You Need To Move To A Supported Option ASAP

TIL there was a vendor providing long-term support versions of Rails updates. I also like the 10 day lag between paid and free updates. Are you feeling lucky you won't get owned inside of 10 days when an exploit gets released?

soullessshoe
Nov 6, 2011
going to be teaching some people ruby/rails, any recommended resources?

I've found:

- http://curriculum.railsbridge.org/curriculum/
- http://tutorials.jumpstartlab.com/

wolffenstein
Aug 2, 2002
 
Pork Pro
I've been using RailsTutorial.org's online book as my learning tool.

Oh My Science
Dec 29, 2008
Check out http://www.codelearn.org/rails-tutorials

It lets you learn basic rails without having to setup a dev environment locally.

soullessshoe
Nov 6, 2011

wolffenstein posted:

I've been using RailsTutorial.org's online book as my learning tool.

Awesome thanks, totally forgot about that book!

wolffenstein posted:

I've been using RailsTutorial.org's online book as my learning tool.

Oh that's neat, but I think I want students to actually set up and use a development environment. Luckily we're setting up an install fest to help students with that the day before the class, but that's something I can probably point them to after the class for them to try.

EAT THE EGGS RICOLA
May 29, 2008

soullessshoe posted:

Oh that's neat, but I think I want students to actually set up and use a development environment. Luckily we're setting up an install fest to help students with that the day before the class, but that's something I can probably point them to after the class for them to try.

Set up a VM and how them how to use Vagrant.

Oh My Science
Dec 29, 2008
I was just going to recommend vagrant as well. Having a standardized environment for students and instructor could potentially save you a lot of pain.

soullessshoe
Nov 6, 2011
haha nice, I am actually testing some vagrant stuff right now to set up an environment for windows folks, though I'll probably ask everyone to go that route.

Adbot
ADBOT LOVES YOU

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

soullessshoe posted:

going to be teaching some people ruby/rails, any recommended resources?

I've found:

- http://curriculum.railsbridge.org/curriculum/
- http://tutorials.jumpstartlab.com/

I did this with non-programmers.

  • Don't waste time with the Ruby Koans
  • Rails for Zombies is kinda poo poo but the exercises are okay to drill with maybe
  • Use Sublime unless you also feel like teaching IDEs with complicated and confusing interfaces or 1970s editors with non-modern interfaces
  • If students bring their own computers, get ready for "fun" when you find out non-Rails-people don't all have MacBooks with Mountain Lion

Good luck!

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