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
Hop Pocket
Sep 23, 2003

stack posted:

What is a better way to default to a value in an erb file?

code:
(some_nested_path(@some_var) unless @some_var.nil?) || not_nested_path
(value.name unless value.nil?) || '...'
I run into this all the time and I should think there is a better way to handle this.

I might put some of that code into a helper. Either way, you can use a ternary operator:

code:
<%= @some_var ? some_nested_path(@some_var) : not_nested_path %>

Adbot
ADBOT LOVES YOU

jonnii
Dec 29, 2002
god dances in the face of the jews
I answered that question on stackoverflow.com not that long ago. There's also some additional interesting conversation.

http://stackoverflow.com/questions/2060561/optional-local-variables-in-rails-partial-templates-how-do-i-get-out-of-the-def

stack
Nov 28, 2000

jonnii posted:

I answered that question on stackoverflow.com not that long ago. There's also some additional interesting conversation.

http://stackoverflow.com/questions/2060561/optional-local-variables-in-rails-partial-templates-how-do-i-get-out-of-the-def

Thanks! I clicked around on stack overflow a bunch but missed this one.

dizzywhip
Dec 23, 2005

dizzywhip fucked around with this message at 21:54 on Nov 9, 2020

NotShadowStar
Sep 20, 2000
Whatever you're doing it sounds like a bad idea and unnecessary complexity. The only thing I can think of is leveraging the DB itself so it'll be DB-type specific. For instance with MySQL:

code:
class User<ActiveRecord::Base
	before_save :find_existing_name

	private


	def find_exisiting_name
		if self.find(:first, :conditions => ['user_name = COLLATE(?)', self.user_name])
			errors.add_to_base "Username must be unique"
		end
	end

end

dizzywhip
Dec 23, 2005

dizzywhip fucked around with this message at 21:54 on Nov 9, 2020

manero
Jan 30, 2006

What about :

code:
validates_uniqueness_of :name, :case_sensitive => false
for a start? In places where I've had to find a model by a field by ignoring case, I'll make a named scope to do the work for me.

Pardot
Jul 25, 2001




Or write your own User#name= method that downcases all the usernames before they're added to the database.

I havent tested it out, but
code:
def name=(new_name)
  super new_name.downcase
end
probably works.

edit: nah that wouldn't do it when you're doing update_attributes, so you'll probably have to make a before_save callback.

Pardot fucked around with this message at 16:22 on May 8, 2010

skidooer
Aug 6, 2001
You have a few options.

code:
class User < ActiveRecord::Base
  scope :sort, order('LOWER(username)')
  validates_uniqueness_of :username, :case_sensitive => false
end
code:
class User < ActiveRecord::Base
  scope :sort, order('lowercase_username')
  validates_uniqueness_of :lowercase_username

  def username_with_lowercase_assignment=(username)
    self.username_without_lowercase_assignment = username.downcase
  end
  alias_method_chain :username=, :lowercase_assignment
end
code:
class User < ActiveRecord::Base
  default_scope select('users.*, LOWER(users.username) AS lowercase_username')
end

dizzywhip
Dec 23, 2005

dizzywhip fucked around with this message at 21:54 on Nov 9, 2020

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

skidooer posted:



code:
class User < ActiveRecord::Base
  scope :sort, order('lowercase_username')
  validates_uniqueness_of :lowercase_username

  def username_with_lowercase_assignment=(username)
    self.username_without_lowercase_assignment = username.downcase
  end
  alias_method_chain :username=, :lowercase_assignment
end

I had no idea about the method_chain, that's awesome.

manero
Jan 30, 2006

Ryouga Inverse posted:

I had no idea about the method_chain, that's awesome.

Treat it like a loaded gun. And don't go crazy with it. Please.

NotShadowStar
Sep 20, 2000

manero posted:

Treat it like a loaded gun. And don't go crazy with it. Please.

Yehuda Katz is, as always, the voice of reason

http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/

skidooer
Aug 6, 2001

mit_senf posted:

I think the syntax
I was using the Rails 3 syntax. But you are right, if you're still on Rails 2, that is the syntax to use.

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.

bitprophet
Jul 22, 2004
Taco Defender
RVM is great, I've found it to be a passable analogue to Python's virtualenv. It's still kinda flaky in spots, mostly just that the invocation syntax is clunky, but it does work.

I used to stay on Ruby 1.8.6 because it has the strongest OS-level support, but since discovering RVM I've been more apt to play with 1.8.7 and all its new shinies, at least for non-server-oriented projects. (Haven't yet looked into how to integrate it with eg Passenger. I assume it can, just like how mod-wsgi Web apps can leverage virtualenv in the Python world, but haven't verified this.)

Combined with Bundler it makes setting up per-project environments and gemsets pretty easy. No more worrying about an enormous list of global gems stomping all over each other.

NotShadowStar
Sep 20, 2000

bitprophet posted:

RVM is great, I've found it to be a passable analogue to Python's virtualenv. It's still kinda flaky in spots, mostly just that the invocation syntax is clunky, but it does work.

I used to stay on Ruby 1.8.6 because it has the strongest OS-level support, but since discovering RVM I've been more apt to play with 1.8.7 and all its new shinies, at least for non-server-oriented projects. (Haven't yet looked into how to integrate it with eg Passenger. I assume it can, just like how mod-wsgi Web apps can leverage virtualenv in the Python world, but haven't verified this.)

Combined with Bundler it makes setting up per-project environments and gemsets pretty easy. No more worrying about an enormous list of global gems stomping all over each other.

http://rvm.beginrescueend.com/integration/passenger/

I used the same passenger_ruby script for a CGI app. Change #!/usr/bin/env ruby to #!~/.rvm/bin/passenger_ruby and you're all set.

There's very little reason to use 1.8 anymore. If there's an abandoned library that won't work on 1.8 I'll try and fix it, I've done that a lot already.

bitprophet
Jul 22, 2004
Taco Defender

NotShadowStar posted:

There's very little reason to use 1.8 anymore.

(Thanks for the confirmation re: Passenger.)

1.9 is still way too new for serious deployment IMO, certainly at any outfit which has to maintain codebases that are more than a year or two old, and/or which deploys to servers not running on the bleeding edge.

And since I tend to not like bouncing between various different versions of X often (where X is a language or a program or OS) I tend to try and pick something that's old enough to be stable and readily available but not SO old that it lacks any feature parity whatsoever. Thus, 1.8.7 fits the bill pretty handily.

(The Web stuff at my job is all still 1.8.6; for us, moving to that version's REE is considered a big step, even. We don't have much manpower available for R&D or support, otherwise we probably would've moved to 1.8.7 by now. Meh.)

bitprophet fucked around with this message at 23:45 on May 15, 2010

NotShadowStar
Sep 20, 2000
One of the way cool features about rvm is you can run a single command against all installed versions of Ruby. For example: your test suite:

code:
rvm install 1.8.7 1.9.1 ree rbx jruby
rvm bundle install
rvm rake test
Unless you don't have any sort of testing then... you've got other problems. Really though making the jump to 1.9 is not that big of a deal at all. There's a lot of noise of 'OH GOD SCARY' but really almost every library works with it now unless it's been long abandoned, and from the non-library developer standpoint it's almost always string slicing and scanning that's different. String manipulations actually makes sense in 1.9 because for example "abcde"[0] actually returns the first character of the string (unicode or not) instead of the byte code that you have to translate.

Evil Trout
Nov 16, 2004

The evilest trout of them all

bitprophet posted:

1.9 is still way too new for serious deployment IMO, certainly at any outfit which has to maintain codebases that are more than a year or two old, and/or which deploys to servers not running on the bleeding edge.

I disagree. I've been running 1.9.1 in production for well over a year on a codebase that's 4 years old. The upgrade took maybe 3 days of work and resulted in a huge speed improvement (average of 2x as fast).

Pardot
Jul 25, 2001




So who else is headed out to Baltimore?

Obsurveyor
Jan 10, 2003

Evil Trout posted:

I disagree. I've been running 1.9.1 in production for well over a year on a codebase that's 4 years old. The upgrade took maybe 3 days of work and resulted in a huge speed improvement (average of 2x as fast).
The thing that pisses me off is that they broke support for 1.9.1 in Rails 3. I am happy running 1.9.1 but I am not happy having to use 1.9.2-trunk. They were targeting the Rails 3 release for this Baltimore conference and I do not know if that has changed but if it goes release with this 1.9.2 requirement, I am going to be extremely annoyed.

NotShadowStar
Sep 20, 2000

Obsurveyor posted:

The thing that pisses me off is that they broke support for 1.9.1 in Rails 3. I am happy running 1.9.1 but I am not happy having to use 1.9.2-trunk. They were targeting the Rails 3 release for this Baltimore conference and I do not know if that has changed but if it goes release with this 1.9.2 requirement, I am going to be extremely annoyed.

Wait what? I've been running Rails 3 on 1.9.1 since like December and have had nary a problem. I tried 1.9.2 the other week and things were horribly broken, WEBRick wouldn't even start on an empty Rails project.

dustgun
Jun 20, 2004

And then the doorbell would ring and the next santa would come

Pardot posted:

So who else is headed out to Baltimore?
Based there, but probably only going to Bohconf. Have fun getting murdered, suckers.

Pardot
Jul 25, 2001




dustgun posted:

Based there, but probably only going to Bohconf.

I'm planning that for the tutorials day

dustgun posted:

Have fun getting murdered, suckers.

:ohdear:

Obsurveyor
Jan 10, 2003

NotShadowStar posted:

Wait what? I've been running Rails 3 on 1.9.1 since like December and have had nary a problem. I tried 1.9.2 the other week and things were horribly broken, WEBRick wouldn't even start on an empty Rails project.
beta2 stopped working for me on 1.9.1. It just segfaults when you try to start up the server. Rails 3 trunk as well.

Ruby on Rails Blog posted:

Note that Ruby 1.8.7 p248 and p249 has marshaling bugs that crash both Rails 2.3.x and Rails 3.0.0. Ruby 1.9.1 outright segfaults on Rails 3.0.0, so if you want to use Rails 3 with 1.9.x, jump on 1.9.2 trunk for smooth sailing.

NotShadowStar
Sep 20, 2000
Beta 3 has been out for a month and a half.

http://rubygems.org/gems/rails

Obsurveyor
Jan 10, 2003

NotShadowStar posted:

Beta 3 has been out for a month and a half.

http://rubygems.org/gems/rails
Same segfaults unless I use 1.9.2. My post about beta2 was just when it started and when they said something. Note my "Rails 3 trunk" too, i.e. the latest version from git(as of Friday when I messed with it last at work) segfaults as well.

Obsurveyor fucked around with this message at 22:23 on May 31, 2010

bitprophet
Jul 22, 2004
Taco Defender

Evil Trout posted:

I disagree. I've been running 1.9.1 in production for well over a year on a codebase that's 4 years old. The upgrade took maybe 3 days of work and resulted in a huge speed improvement (average of 2x as fast).

I guess it depends a lot on where you work; I rarely have the free time to sink hours/days into a project for upgrading unless there's a need, and there's significant pressure to avoid needless hours billed to clients (or eaten internally). So in that kind of environment, one settles on a specific (old, readily available on most Unix distros) version and stick with it for quite a while :(


Re: 1.8.7 marshal segfaults: I just got bitten by those recently, albeit in Sass (and via Nanoc, not Rails). Thankfully RVM let me set up 1.8.7-HEAD easily which seems to have fixed the issue.

Speaking of RVM, anybody using the latest-n-greatest rip (i.e. the prerelease v2)? Still not a big fan of RVM's clunkiness w/r/t gemsets.

Mrs. Wynand
Nov 23, 2002

DLT 4EVA
if you're not switching ruby executables as well, is there any reason why it would be preferable to use rvm over simply vendorizing all your gems?

Pardot
Jul 25, 2001




Mr. Wynand posted:

if you're not switching ruby executables as well, is there any reason why it would be preferable to use rvm over simply vendorizing all your gems?

You can use gemsets sorta like bundler so you know exactly all the gmes your app sees and their versions.

That said, after a (very) rocky start, bundler has gotten stable enough for me. We haven't had any huge problems for some time, and it works great for making sure dev, production, and ci all have the same poo poo.

jetviper21
Jan 29, 2005

Pardot posted:

So who else is headed out to Baltimore?

I'm based in Baltimore also. Hint I'm the only fat one with a bmore on rails shirt on

asveepay
Jul 7, 2005
internobody

Obsurveyor posted:

Same segfaults unless I use 1.9.2. My post about beta2 was just when it started and when they said something. Note my "Rails 3 trunk" too, i.e. the latest version from git(as of Friday when I messed with it last at work) segfaults as well.

did you remove beta 1? There are a ton of errors if you try to keep both installed.

Obsurveyor
Jan 10, 2003

asveepay posted:

did you remove beta 1? There are a ton of errors if you try to keep both installed.
Yep, I wipe my gems every time I install a new beta. I am going to try beta4 today and see if it is any better.


edit: Well, this does not give me a lot of faith it is going to work:

Yahuda Katz posted:

We're not currently supporting 1.9.1, but we should possibly stop Rails 3 itself from booting on 1.9.1. We've gotten a bunch of obscure 1.9.1 bugs and have a lot of trouble keeping CI running on 1.9.1, but this is not the case about 1.9.2.
I mean, I have no trouble running 1.9.2 myself, given I do not run into bugs, since I can run my own server but what about all those hosting services out there? Will this not cut into Rails 3 adoption?

Obsurveyor fucked around with this message at 16:32 on Jun 10, 2010

NotShadowStar
Sep 20, 2000
That's just a lovely situation. 1.9.2 is known buggy and not in a releasable state yet, but if 1.9.1 has bugs that makes Rails 3 not working then...

The 1.9.1 => 1.9.2 leap shouldn't be nearly as painful as 1.8 => 1.9 though, so as soon as 1.9.2 hits stable things should look up quite a bit.

Obsurveyor
Jan 10, 2003

Looks like they went ahead with Katz' idea(from rails3 head):
code:
Rails 3 does not work with Ruby 1.9.1. Please upgrade to 1.9.2.
Beta 4 new app ran on 1.9.1 for me but from the comments on the Ruby on Rails blog, it looks like it has places where it can segfault 1.9.1.

I still do not agree with them targeting an unstable and unreleased version of Ruby but I am not going to sweat it until I run into issues(:love: rvm). The other thing is, I thought odd versions of Ruby are development version and even are stable? If so, it could be a long time until there is a "stable" release.

Obsurveyor fucked around with this message at 21:10 on Jun 10, 2010

Pardot
Jul 25, 2001




jetviper21 posted:

I'm based in Baltimore also. Hint I'm the only fat one with a bmore on rails shirt on

I didn't see is until too late, but bohconf was cool, thanks.

SmutAnEggs
Jan 1, 2006
I have been tasked with updating/rewriting a check out system that we use. The system resembles a library check out system.
Currently the system uses a 4 digit unique identifier for each piece of equipment. The system needs to be updated to be able to handle any kind of unique identifier, we are switching to a to a new numbering system/barcode system.
The system will now need to be able to handle the use of USB based scanner, for checking in and out.

The use of the system will break down like this:
1.) A person will pick out a piece of equipment.
2.) They will bring it to me(or others that have the authority to check out) and ask to be checked out.
3.) I will scan the piece of the equipment with the barcode on it with the USB scanner.
4.) I will enter in their unique id to associate them with the check out of the equipment.
5.) I will enter in how long they want the piece of equipment checked out for.
6.) I will enter my username and password and check out said person.
7.) The person doesn't return the item in time based on the timeline they stated when they checked it out.
8.) Email will get dispatched to the user stating that they have the piece of equipment checked out and it will need to be returned.
9.) User brings the equipment back. I scan the barcode on the equipment with the USB scanner, enter in my username and password. Check in the piece of the equipment.

I will also need to update equipment from the old system to the new system. When I find a piece of equipment that doesn't have the new unique identifier associated with it(not all at one time).

I was shown the code/guts of the current system breifly, and had it dropped in my lap. I guess it uses Ajax, PHP, MySQL. I was told that the old system is worth scrapping and starting over on a new system is best I guess the implementation of the current system used a 4 digit identifier hard coded all over the place. I haven't looked into it....

I'm wondering if RoR is what I should use to for this project? I have not ever done any web development before.... I have coded in C++ and Java in the past ,but all real time. Never any database programming. I see that Agile Web Development with Rails book is recommended and I will pick this up if RoR is what I'm looking for?
Where would I look for the handling of the USB scanner, could this be implemented in RoR?

NotShadowStar
Sep 20, 2000
USB barcode scanners are almost always just another keyboard input. They translate a barcode (which is just an encoding of letters into something the scanner can read) into keystrokes. So that would be a matter of focusing on the appropriate text box on the form and scanning the code.

As for the rest, yes I can picture how it works in Rails quite easily... but if you haven't done web development before Rails can be quite confusing. It makes things a whole lot easier, organized, faster to build than, say, PHP but it also assumes you've already built stuff before. Web development is a weird integration of disparate technologies that has lots of failure points, some entirely beyond your control. If you have experience in a M-V-C or M-V-P style program then you'll pick it up faster, but if not then you're going to have a difficult time.

Adbot
ADBOT LOVES YOU

8ender
Sep 24, 2003

clown is watching you sleep
I've been on Java web development big time the past couple years, before that PHP and PL/SQL, but something about Rails keeps drawing me back ever since I went through a few chapters of a Rails book. More recently I was playing around with Codeigniter for a side project and suddenly I had a yearning to get Railed.

I have an old copy of Agile Development with Rails 2nd Edition. It seems to cover Rails 1.9. Would starting with this book put me in a bad place given that Rails 2 is mature and Rails 3 is imminent? Should I look around online for some Rails 2 tutorials?

I primarily work in Struts2 on Java right now so a lot of the strong MVC concepts here ring my bell and I'm comfortable with them already. Its mainly the syntax and... looseness.. that is throwing me.

8ender fucked around with this message at 21:20 on Jun 14, 2010

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