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
The Journey Fraternity
Nov 25, 2003



I found this on the ground!

KoRMaK posted:

My one major tip: learn where your gem libs are and dig around in them. Sometimes the documentation is on the github site, but if you get into the code you'll see all sorts of helpful comments. I had to do this with attr_encrypted when we realized that it wasn't using IV and Salt. Someone forked it, made the improvement, and then it was merged back into attr_encrypted but no documentation talked about it. I eventually got into the library and started doing searches for keywords and found what I needed.

The other thing I did was made my own helper function, I extended serialize to has_many_serialized and now I can use that in my models. I was helped out by looking at how serialize is defined (seen here https://github.com/rails/rails/blob...erialization.rb )



Changing topics, I just had to wrestle with rails to get it to update one column. Doing the following resulted in no changes being sent to the db
Ruby code:
#my_things_controller.rb
def my_function
  my_thing.title[2] = params[:third_character] #replace the third character with the one sent from params
  my_thing.save
end
I tried a couple variations of that and it didn't work. I had to finally rest on using update_column
Ruby code:
def my_function
  _title = my_thing.title
  _title[2] = params[:third_character] #replace the third character with the one sent from params
  my_thing.update_column(:title, _title)
end
That's fucky as poo poo. Nearest I can tell is that the change of one character didn't make the object realize that it had changes on that column, so it didn't do an update query including that column. I watched the sql statements in the console, and the title never got touched until update_column was used.

You'd want to call "title_will_change!" at the top of my_function unless the character was the same.

Ruby code:
#my_things_controller.rb
def my_function
  my_thing.title_will_change! unless title[2] == params[:third_character]
  my_thing.title[2] = params[:third_character] #replace the third character with the one sent from params
  my_thing.save
end

Adbot
ADBOT LOVES YOU

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

KoRMaK posted:

Oh, okay. Thanks! Also, why? Is _title and @my_thing.title the same object in memory? I thought doing _title = @my_thing.title would create a new instance of the string. And when I set @my_thing.title = _title it would see that there was a different string set.

When you're doing assignment in Ruby, it's by reference- two names pointing to the same thing.

Ruby code:
x = 'hello'
y = x
x == y  # true
x[1] = '3'
x == y  # true
This is the reason there are two versions of some methods, like Enumerable#sort and Enumerable#sort!- sort returns a sorted copy without modifying the original, and sort! modifies that instance in-place.

If you straight-up need copies and you know you do, look at Object#dup and Object#clone.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
I use vim.vroom, with it's "run current file's spec" command bound to leader-r. Maybe not as fast as running a single spec, but not by much at all, and I still get vim's goodness.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

enraged_camel posted:

I'm working on a hobby CRUD project that allows users to upload stuff and then view their submissions.

Instead of the typical "/resources/:id" path, where id is the primary key, I'm thinking about creating a random url-safe base 64 string and associating it with each record.

My challenge: I don't want to store these strings as part of the record. I would like the lookups to still be done using the primary key.

Can I use something like Redis for this? I was thinking of a structure like { string => id } whereby during every request I feed it the string (which is the request param), find the corresponding id, and then use that id to find and display the record.

That would work, yes, but like Smol in curious what the use case is here. Why don't you want that slug in your database?

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
PHP thread is thataway

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

KoRMaK posted:

What is that?

Plain Old Ruby Objects.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

big boi posted:

This is awesome, I'm going to use #blank? all over the place now.

BTW I just found this thread, looks like an awesome resource. I'm a student at the Turing School in Denver, which is like a long-form (7 mo) bootcamp for Ruby/Rails/JS. If anyone knows about anyone hiring junior devs in the Denver metro area or remotely, let me know! Otherwise I'll just be lurking and putting my two cents in occasionally.

Casimir runs a pretty good program- we hired one of his grads a little over a year ago.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
Could always look at a site like http://www.ruby-toolbox.com to see what the most popular ones are, then dive into one on Github to see what its open issues are.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

KoRMaK posted:

If I wanted to spawn a process from my app that runs continuously, how would I do that and make sure it' stays running, or gets started again if its not? My rough draft is that I would spawn a SuckerPunch job that would just continuously run with a do while(true) loop, and sleep when it didn't have stuff to do.

You'll also want something like god/monit/whatever the new hotness is if you want to ensure that the process is constantly running.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

This is everything I never knew I needed.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

The Dreamer posted:

I'm trying to randomly grab a field from a randomly selected record that is associated with another record. I see that I can do this by ordering and calling a Random function for whichever database I'm using. This is still a prototype app so I'll probably actually do this before I go to production. I found a different way to do this though and was wondering if it was actually a terrible idea. It works now but I'm not sure how it would scale over time with larger database tables.

I have 3 tables, Business, Types, and BusinessTypes. I'm doing a Business has_many Types though BusinessTypes association. What I'm trying to do right now is grab a random Business of a certain Type and grab an image url that is stored in the Business object. my current way of doing this is:

code:
type.businesses[rand(0...type.businesses.count)][:image]
Like I said this actually works but I'm just curious as to how it would scale. I know that just randomizing the order of the database files and then picking one can be really intense on the database with enough records but this way doesn't seem like it would be since I'm just randomly picking a number and then snagging that index out of the array of Business objects. I could be very wrong though especially if it has to create the whole array of objects every time its called. I just don't know enough about how ActiveRecord works to say one way or the other and wanted to know if someone could enlighten me.

You could do a quick COUNT(*) then choose a random record using rand(count) as your offset and a LIMIT 1

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Pollyanna posted:

There is always more, and it is always worse.

Let us see `rake stats` :v:

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
^^ :puckout:

code:
+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |  2459 |  1852 |      23 |     244 |  10 |     5 |
| Helpers              |   198 |   163 |       0 |      16 |   0 |     8 |
| Models               |   922 |   729 |      23 |     113 |   4 |     4 |
| Mailers              |     0 |     0 |       0 |       0 |   0 |     0 |
| Javascripts          |  1981 |  1164 |       0 |     139 |   0 |     6 |
| Libraries            |   358 |   294 |       1 |      46 |  46 |     4 |
| Controller specs     |   715 |   535 |       0 |       0 |   0 |     0 |
| Helper specs         |    21 |    17 |       0 |       0 |   0 |     0 |
| Model specs          |  1087 |   813 |       0 |       2 |   0 |   404 |
| Request specs        |   834 |   605 |       0 |      14 |   0 |    41 |
| Routing specs        |    39 |    29 |       0 |       0 |   0 |     0 |
| View specs           |    85 |    31 |       0 |       1 |   0 |    29 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                |  8699 |  6232 |      47 |     575 |  12 |     8 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 4202     Test LOC: 2030     Code to Test Ratio: 1:0.5

Still not as bad as I'd expect from those nutjob snippets.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
Am I the only person left in TYOOL 2016 that manually adds every JS library that he uses? Granted, I don't use much, but still.

The Journey Fraternity fucked around with this message at 05:37 on Jun 15, 2016

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

necrotic posted:

They've finally have first-class foreign key support in ActiveRecord migrations! I think the rails5 release is focused mostly on performance improvements and bug fixes over new features, with ActionCable being the big exception.

So just pure improvements since if I ever need sockets I'm gonna be using Phoenix :v:

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
New Turbolinks is the tits.

That is all.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
Ruby on Rails Love-In: Office Space now a reality

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Pardot posted:

Whew, looks like Matz said no to this and it's rolled back https://bugs.ruby-lang.org/issues/12958#note-18 :sweatdrop:

Nice to see sanity win one.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
I may have considered using STI along with Postgres’s table inheritance at one point but I’ve since stopped thinking about the monstrosity that spawned that idea.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Which puts our massive codebase completely out of any support.

God I need a new job.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Sivart13 posted:

you don't want your job to be upgrading the massive codebase to Rails 6?

what kind of job is this that has all the rails but not wants to upgrade it

I would love to, if it weren't such a behemoth undertaking even to get it to 5.0. This codebase predates every single engineer, and was originally written by PHP devs writing their first Rails app.

Adbot
ADBOT LOVES YOU

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

kayakyakr posted:

1) you do need a new job
2) I don't have much empathy, at least for your company, because you didn't spend any time maintaining your codebase. If you had followed along with rails versions, it wouldn't be such a massive undertaking. The longer you put it off, the more painful it will become.

Also, Rails 4 to rails 6 isn't so hard. The codebase was mature enough by Rails 4.2 that there aren't many barriers to changing over.

Oh for sure, I have none for them either. This place is very reactionary and only upgrades when things fall out of security fix windows, giving no time to do it outside of that small window.

(I'll stop the derail here)

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