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
Slow News Day
Jul 4, 2007

The main use case is that I want to learn Redis. :) That's why I'm using it in a hobby project as opposed to a work project.

I also figured it would be faster that way. So my theory is that (Redis hash lookup + database lookup on indexed int column) is faster than (database lookup on indexed string column). I may be wrong, though.

Adbot
ADBOT LOVES YOU

kayakyakr
Feb 16, 2004

Kayak is true

enraged_camel posted:

The main use case is that I want to learn Redis. :) That's why I'm using it in a hobby project as opposed to a work project.

I also figured it would be faster that way. So my theory is that (Redis hash lookup + database lookup on indexed int column) is faster than (database lookup on indexed string column). I may be wrong, though.

Try doing the entire project redis backed, then. If you're going to redis, might as well EXTREME redis.

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.

kayakyakr posted:

Try doing the entire project redis backed, then. If you're going to redis, might as well EXTREME redis.

Word. Setting yourself limits like that is awesome for creativity.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
The hashids gem is pretty neat if you don't want to expose primary keys (but I realize this is all in service of a fun and educational constraint you've set for yourself)

Pollyanna
Mar 5, 2005

Milk's on them.


If I have a set of static pages on my site, should I make a separate StaticController for them, or should I let ApplicationController handle them?

kayakyakr
Feb 16, 2004

Kayak is true

Pollyanna posted:

If I have a set of static pages on my site, should I make a separate StaticController for them, or should I let ApplicationController handle them?

I'd consider rendering them and putting them in public.

If you don't want to pre-render them, I'd suggest creating a controller to handle that. Application really should only contain things that you want to be on ALL controllers. I usually make a MainController to handle my site index and various other semi-static pages.

Thalagyrt
Aug 10, 2006

kayakyakr posted:

I'd consider rendering them and putting them in public.

If you don't want to pre-render them, I'd suggest creating a controller to handle that. Application really should only contain things that you want to be on ALL controllers. I usually make a MainController to handle my site index and various other semi-static pages.

Seems to be a common design decision. I have a PagesController to handle the static pages. They all have a bit of dynamic content in them, so they have to be dynamically rendered.

I'm thinking of splitting it into a Pages module with a LandingsController, FeaturesController, so on and so forth so I can have a bit more structure to the static pages... All of these static pages in one directory is getting kind of unwieldy!

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Pollyanna posted:

If I have a set of static pages on my site, should I make a separate StaticController for them, or should I let ApplicationController handle them?

Use High Voltage to render and templatize the static pages.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
What's the best way to model ordering within a scoped set of records? The project I'm working on is, roughly speaking, a survey app - Question is a model, as is QuestionSet. Currently, each Question record has a position field that is unique scoped to a QuestionSet. This represents, as expected, the order of each question within the particular survey (QuestionSet).

The problem comes in reordering - if you want to make the first question the second instead - it's necessary to modify more than one Question's position field, as the changes can cascade - in some cases to as much as every other Question in the QuestionSet. This seems unnecessary - the position integers needn't be sequential - they are only necessary to denote relative ordering. I see two possible solutions:

1) Mark the positions with large, non-adjacent integers: 100,000, 200,000, etc. Then it's possible to do any reordering of any one record and only do a save on that one record.

2) Model this in an entirely different way, and don't make the position an attribute of Question (as it is logically an attribute of QuestionSet - how that particular set displays its questions).

Any thoughts?

Peristalsis
Apr 5, 2004
Move along.

Lexicon posted:

What's the best way to model ordering within a scoped set of records? The project I'm working on is, roughly speaking, a survey app - Question is a model, as is QuestionSet. Currently, each Question record has a position field that is unique scoped to a QuestionSet. This represents, as expected, the order of each question within the particular survey (QuestionSet).

The problem comes in reordering - if you want to make the first question the second instead - it's necessary to modify more than one Question's position field, as the changes can cascade - in some cases to as much as every other Question in the QuestionSet. This seems unnecessary - the position integers needn't be sequential - they are only necessary to denote relative ordering. I see two possible solutions:

1) Mark the positions with large, non-adjacent integers: 100,000, 200,000, etc. Then it's possible to do any reordering of any one record and only do a save on that one record.

2) Model this in an entirely different way, and don't make the position an attribute of Question (as it is logically an attribute of QuestionSet - how that particular set displays its questions).

Any thoughts?

I think using large, non-adjacent integers is kind of an ugly hack, and in theory, it will only work until you do enough re-ordering to fill up one of the gaps.

Have you considered giving each question a reference to the next question, instead of an absolute position? Then you're basically just maintaining a linked list. There may be (probably are) better ways, but I think even this is better than using huge counters.

kayakyakr
Feb 16, 2004

Kayak is true

Peristalsis posted:

Have you considered giving each question a reference to the next question, instead of an absolute position? Then you're basically just maintaining a linked list. There may be (probably are) better ways, but I think even this is better than using huge counters.

If he's trying to avoid making multiple saves, this could make things worse: if you build a doubley-linked list, you potentially have to update 5 records. OTOH, at least it would be a consistent and known number of saves (4-5).

Lexicon posted:

What's the best way to model ordering within a scoped set of records? The project I'm working on is, roughly speaking, a survey app - Question is a model, as is QuestionSet. Currently, each Question record has a position field that is unique scoped to a QuestionSet. This represents, as expected, the order of each question within the particular survey (QuestionSet).

The problem comes in reordering - if you want to make the first question the second instead - it's necessary to modify more than one Question's position field, as the changes can cascade - in some cases to as much as every other Question in the QuestionSet. This seems unnecessary - the position integers needn't be sequential - they are only necessary to denote relative ordering. I see two possible solutions:

1) Mark the positions with large, non-adjacent integers: 100,000, 200,000, etc. Then it's possible to do any reordering of any one record and only do a save on that one record.

2) Model this in an entirely different way, and don't make the position an attribute of Question (as it is logically an attribute of QuestionSet - how that particular set displays its questions).

Any thoughts?

2 suggestions:

1) do it how you're doing it but don't worry about saving multiple records, it's not that resource heavy. Unless your question sets have 500+ questions in them, it's no big deal. How often are questions going to be reordered in your app?

2) switch to a document database and keep all questions embedded on the question set. This is a bit extreme.

3) keep the order of questions embedded in the question set: an array of ID's, sorted how it should appear. Then you can get the sort order by mirroring the array. This is a little bit more resource heavy on the read-side as your sort will become more complex. This becomes less efficient with larger question sets as well.

I would suggest doing #1 and just have a slightly less efficient save of a resort. Unless your use-case is just these massive question sets that are reordered as a primary function of the application, it's not going to strain your server too hard.

Pardot
Jul 25, 2001




Peristalsis posted:

Have you considered giving each question a reference to the next question, instead of an absolute position? Then you're basically just maintaining a linked list. There may be (probably are) better ways, but I think even this is better than using huge counters.

Yeah I'd do linked lists and make it fancy with recursive CTE queries, but that's me.

KoRMaK
Jul 31, 2012



Is use of "define_method" in ruby an example of self modifying code?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
Thanks for the thoughts everyone. I love the LinkedList idea in particular.

Jaded Burnout
Jul 10, 2004


Lexicon posted:

Thanks for the thoughts everyone. I love the LinkedList idea in particular.

What happens if two people reorder them at the same time? Or two people delete one?

One of the advantages of always doing a bulk update is it's self repairing when something goes wrong.

Jaded Burnout fucked around with this message at 20:57 on Aug 18, 2014

Jaded Burnout
Jul 10, 2004


KoRMaK posted:

Is use of "define_method" in ruby an example of self modifying code?

I suppose? Sort of?

It's metaprogramming, but I guess you could use it to redefine methods in slightly funky ways. What's the context?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Arachnamus posted:

What happens if two people reorder them at the same time? Or two people delete one?

One of the advantages of always doing a bulk update is it's self repairing when something goes wrong.

I'm not too worried about that, as I was going to run it in a transaction anyhow, and this will be done in a little-used CMS.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

KoRMaK posted:

Is use of "define_method" in ruby an example of self modifying code?

When I was a C++ programmer and had never done any dynamic language stuff, "code that defines a method and then calls it" would have been all sorts of :monocle: to me.

"self-modifying code" was one of those terms that used to be thrown around by cane-wavers to describe the sorts of things that could be done in these newfangled languages the drat kids are using.

I wouldn't say it's a well-defined technical term though.

KoRMaK
Jul 31, 2012



Arachnamus posted:

I suppose? Sort of?

It's metaprogramming, but I guess you could use it to redefine methods in slightly funky ways. What's the context?

Context: saw it described in a coding style-guide for C++ regarding the F35B. Realized I didn't actually know precisely what it meant and started looking for examples. Page 12 mentions it in the pdf linked below.

Sauer posted:

If you know C++ feel free to adopt the JSF's Coding Standards; all 142 pages of it. Much of it is wall papering over the weaknesses of the particular version of the C++ Standard they're using.

Lexicon posted:

When I was a C++ programmer and had never done any dynamic language stuff, "code that defines a method and then calls it" would have been all sorts of :monocle: to me.

"self-modifying code" was one of those terms that used to be thrown around by cane-wavers to describe the sorts of things that could be done in these newfangled languages the drat kids are using.

I wouldn't say it's a well-defined technical term though.
I agree. That's currently the boat I'm in. I started on C++ and this kind of stuff was mythical to me in that language. I thought I had seen the effects of it, but never got to understand it myself.

Jaded Burnout
Jul 10, 2004


KoRMaK posted:

Context: saw it described in a coding style-guide for C++ regarding the F35B. Realized I didn't actually know precisely what it meant and started looking for examples. Page 12 mentions it in the pdf linked below.

Yeah that's probably the sort of thing he's talking about. Anything that patches or redefines classes at runtime.

KoRMaK posted:

I started on C++ and this kind of stuff was mythical to me in that language. I thought I had seen the effects of it, but never got to understand it myself.

Ruby metaprogramming is a deep hole you can dig yourself down (everything is an instance of a class, including classes themselves) and can be quite exciting, but you'll generally not use it very often for the reasons mentioned in the PDF; it's very easy to produce horribly incomprehensible and untraceable code.

The most common case you'll see it used day to day is in DSLs, like RSpec, Cucumber, ActiveRecord. Whenever you write "has_many :posts" it's calling a superclass method (on ActiveRecord::Base) which is defining a whole host of methods on your model class, like getters, setters etc. Do this for enough attributes, validations etc and you can see how the public interface to your class quickly gets huge and out of your control.

For this reason, DSLs are falling out of favour with a movement towards plain Ruby objects, which are much easier to test.

Jaded Burnout fucked around with this message at 22:31 on Aug 18, 2014

KoRMaK
Jul 31, 2012



Arachnamus posted:

Yeah that's probably the sort of thing he's talking about. Anything that patches or redefines classes at runtime.


Ruby metaprogramming is a deep hole you can dig yourself down (everything is an instance of a class, including classes themselves) and can be quite exciting, but you'll generally not use it very often for the reasons mentioned in the PDF; it's very easy to produce horribly incomprehensible and untraceable code.

The most common case you'll see it used day to day is in DSLs, like RSpec, Cucumber, ActiveRecord. Whenever you write "has_many :posts" it's calling a superclass method (on ActiveRecord::Base) which is defining a whole host of methods on your model class, like getters, setters etc. Do this for enough attributes, validations etc and you can see how the public interface to your class quickly gets huge and out of your control.

For this reason, DSLs are falling out of favour with a movement towards plain Ruby objects, which are much easier to test.
:smith: I just started going hog while making libraries and modules that use define_method with dynamically generated methods. It's great, I've written a handful of "has_many" style class methods that reduce a bunch of boilerplate code.

I suppose I could make a class that doesn't use the dynamic method names and instead keeps track of a list of fields that a certain idiom applies to.

xtal
Jan 9, 2011

by Fluffdaddy

KoRMaK posted:

It's great, I've written a handful of "has_many" style class methods that reduce a bunch of boilerplate code.

Reducing boilerplate is good metaprogramming. find_by_.. is bad metaprogramming. It's a delicate balance that I think focuses on keeping the explicit explicit and avoiding so-called magic.

(In your case, aren't you reimplementing modules and include?)

xtal fucked around with this message at 01:43 on Aug 19, 2014

KoRMaK
Jul 31, 2012



xtal posted:

Reducing boilerplate is good metaprogramming. find_by_.. is bad metaprogramming. It's a delicate balance that I think focuses on keeping the explicit explicit and avoiding so-called magic.
Well, uh, to be honest.

My module builds a bunch of find_by.. style things. Mine are for jquery tokenizers. They build a json object for them. They all take the form tokenize_.. and automate the ActiveRecord and other relationship methods and different ways to render them that the rest of the app can automatically expect and automate a lot of individual file editing I would have to do.

I wrote one file, and now all I have to do is my_thingy :field_name and it saves me like 45 minutes a piece. About 13 or more models use this feature.

xtal posted:

(In your case, aren't you reimplementing modules and include?)
I'm not sure what you mean, can you clarify?

xtal
Jan 9, 2011

by Fluffdaddy

KoRMaK posted:

Well, uh, to be honest.

My module builds a bunch of find_by.. style things. Mine are for jquery tokenizers. They build a json object for them. They all take the form tokenize_.. and automate the ActiveRecord and other relationship methods and different ways to render them that the rest of the app can automatically expect and automate a lot of individual file editing I would have to do.

It's okay to use a has_many method to create several methods dynamically. It's not okay to overload method_missing to do (e.g.) regular expression matching and dispatching on dynamic data. Things should be explicit, opt-in and understandable, but it's hard to describe exactly where that line is.

quote:

I'm not sure what you mean, can you clarify?

If you're using metaprogramming to DRY up method definition with static data you might as well just put those things in a module. But that doesn't seem to apply to your case.

Peristalsis
Apr 5, 2004
Move along.

Arachnamus posted:

What happens if two people reorder them at the same time? Or two people delete one?

One of the advantages of always doing a bulk update is it's self repairing when something goes wrong.

Sorry to necro this, but what do you mean? Are you basically concerned with record locking? If so, wouldn't that be an issue for any update method?

Jaded Burnout
Jul 10, 2004


Peristalsis posted:

Sorry to necro this, but what do you mean? Are you basically concerned with record locking? If so, wouldn't that be an issue for any update method?

It's a concern for any update method, yeah, but especially for one which is trying to maintain a linked list. If you're doing a mass update then a user might blat another user's changes but at least they won't leave it in a totally broken and unrecoverable (from their interface) state.

Peristalsis
Apr 5, 2004
Move along.

Arachnamus posted:

It's a concern for any update method, yeah, but especially for one which is trying to maintain a linked list. If you're doing a mass update then a user might blat another user's changes but at least they won't leave it in a totally broken and unrecoverable (from their interface) state.

Thanks, that makes sense.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
I'm in the process of refactoring and cleaning up a codebase that uses a lot of metaprogramming and from now until the forever future i will never metaprogram unless whatever I'm doing will be loudly documented.

Which sucks, because metaprogramming is the most fun you can have as a programmer. It's programming for programmers.

Slow News Day
Jul 4, 2007

So a new project got thrown at me today, and frankly it scares me a little. It involves moving a Rails app I developed from Heroku to my company's infrastructure. This is so that the app can take advantage of single-sign-on in the domain and also talk to our main customer database. Essentially, customers already have accounts on mycompany.com that the app needs to leverage.

I was thinking about the problem earlier, and the three main challenges seem to be:

1. Learning Linux and PostgreSQL administration, i.e. everything Heroku is doing right now and I've been taking for granted!
2. Changing the app's authentication scheme so that it stops handling that itself and instead delegates it to another service (I have no idea where to even start with this)
3. Switching from Amazon S3 to... something internal. I don't know what.

The IT team basically said, "yeah we don't have anyone who knows Linux too well but we can spin up an Ubuntu VM and give you access" so it doesn't sound like I'll be getting much support from them.

I'm hosed, right? :ohdear:

Safe and Secure!
Jun 14, 2008

OFFICIAL SA THREAD RUINER
SPRING 2013
So let's say I have Book records, where each record has

Id (int), Title (String), DuplicateTitleAllowed (boolean).

There may any number of books with the same Title so long as no records with that Title exist that have DuplicateTitleAllowed set to false. Of course, we can't create records with that flag set to false if its title collides with another record that happens to allow duplicates.

How do I enforce this entirely from within Rails without adding any constraints, triggers, etc. to my database? How do I do it without using an after_commit callback that removes duplicate records? Note that an after_save check won't work, because two transactions may happen at the same time, resulting in the check not seeing duplicates in the database.

I don't want any more callbacks and I don't want to explicitly lock an table. I'm using MySql 5.1 with InnoDB, if that helps.

Stringent
Dec 22, 2004


image text goes here

enraged_camel posted:

So a new project got thrown at me today, and frankly it scares me a little. It involves moving a Rails app I developed from Heroku to my company's infrastructure. This is so that the app can take advantage of single-sign-on in the domain and also talk to our main customer database. Essentially, customers already have accounts on mycompany.com that the app needs to leverage.

I was thinking about the problem earlier, and the three main challenges seem to be:

1. Learning Linux and PostgreSQL administration, i.e. everything Heroku is doing right now and I've been taking for granted!
2. Changing the app's authentication scheme so that it stops handling that itself and instead delegates it to another service (I have no idea where to even start with this)
3. Switching from Amazon S3 to... something internal. I don't know what.

The IT team basically said, "yeah we don't have anyone who knows Linux too well but we can spin up an Ubuntu VM and give you access" so it doesn't sound like I'll be getting much support from them.

I'm hosed, right? :ohdear:

As long as people aren't trying to rush you you should be fine. Most of the problems you're going to encounter should be pretty well documented online.

Jaded Burnout
Jul 10, 2004


enraged_camel posted:

2. Changing the app's authentication scheme so that it stops handling that itself and instead delegates it to another service (I have no idea where to even start with this)

You can probably crib a bunch of ideas and/or code from these:
https://github.com/alphagov/gds-sso/
https://github.com/alphagov/signonotron2

enraged_camel posted:

3. Switching from Amazon S3 to... something internal. I don't know what.

Why not keep using S3?

Jaded Burnout
Jul 10, 2004


Safe and Secure! posted:

How do I enforce this entirely from within Rails without adding any constraints, triggers, etc. to my database? How do I do it without using an after_commit callback that removes duplicate records? Note that an after_save check won't work, because two transactions may happen at the same time, resulting in the check not seeing duplicates in the database.

You can't. Rails validations are always at risk for race conditions when not backed by database constraints.

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

On that topic, it looks like Rails is finally getting builtin support for foreign keys! http://weblog.rubyonrails.org/2014/8/20/Rails-4-2-beta1/

(Until that lands, everyone should probably be using foreigner)

kayakyakr
Feb 16, 2004

Kayak is true

enraged_camel posted:

So a new project got thrown at me today, and frankly it scares me a little. It involves moving a Rails app I developed from Heroku to my company's infrastructure. This is so that the app can take advantage of single-sign-on in the domain and also talk to our main customer database. Essentially, customers already have accounts on mycompany.com that the app needs to leverage.

I was thinking about the problem earlier, and the three main challenges seem to be:

1. Learning Linux and PostgreSQL administration, i.e. everything Heroku is doing right now and I've been taking for granted!
2. Changing the app's authentication scheme so that it stops handling that itself and instead delegates it to another service (I have no idea where to even start with this)
3. Switching from Amazon S3 to... something internal. I don't know what.

The IT team basically said, "yeah we don't have anyone who knows Linux too well but we can spin up an Ubuntu VM and give you access" so it doesn't sound like I'll be getting much support from them.

I'm hosed, right? :ohdear:

you could if you wanted to set up dokku and use that for deployments. It basically gives you a mini-heroku locally. The downside to that is that if you want to do anything slightly out of the norm, it will be a painful process. I went back to using capistrano for my deployments.

As long as you don't have a deadline, just take your time with it and learn as you go. You'll figure it out.

Also yeah, keep using s3.

Pardot
Jul 25, 2001




If you're not going to use heroku's postgres service anymore, please please please use wal-e which makes minutely or 16mb (whichever is first) off-machine archives of postgres https://github.com/wal-e/wal-e (which is on every hpg database)

Slow News Day
Jul 4, 2007

Thank you guys. I'm no longer contemplating suicide. :gbsmith:

EVGA Longoria
Dec 25, 2005

Let's go exploring!

enraged_camel posted:

So a new project got thrown at me today, and frankly it scares me a little. It involves moving a Rails app I developed from Heroku to my company's infrastructure. This is so that the app can take advantage of single-sign-on in the domain and also talk to our main customer database. Essentially, customers already have accounts on mycompany.com that the app needs to leverage.

I was thinking about the problem earlier, and the three main challenges seem to be:

1. Learning Linux and PostgreSQL administration, i.e. everything Heroku is doing right now and I've been taking for granted!
2. Changing the app's authentication scheme so that it stops handling that itself and instead delegates it to another service (I have no idea where to even start with this)
3. Switching from Amazon S3 to... something internal. I don't know what.

The IT team basically said, "yeah we don't have anyone who knows Linux too well but we can spin up an Ubuntu VM and give you access" so it doesn't sound like I'll be getting much support from them.

I'm hosed, right? :ohdear:

https://github.com/progrium/dokku is a new plan to replicate Heroku's functionality as Docker, so you can deploy wherever you want. Might ease the transition a bit.

KoRMaK
Jul 31, 2012



I want to pass a the arguments from one function directly into another as they are. I'm using a splat operator. How do I do that without having to parse the arguments?

I'm trying the following, but it's not working. I just simply want to pass the parameters, whatever they are and however many there are in the same order to the next method.
Ruby code:
def split(*args)
  self.to_s.split(args)
end
e: Oh, buried in here I found it. Add * to the call
Ruby code:
def split(*args)
  self.to_s.split(*args)
end
http://endofline.wordpress.com/2011/01/21/the-strange-ruby-splat/

MAGIC

KoRMaK fucked around with this message at 18:05 on Aug 26, 2014

Adbot
ADBOT LOVES YOU

Jaded Burnout
Jul 10, 2004


KoRMaK posted:

I want to pass a the arguments from one function directly into another as they are. I'm using a splat operator. How do I do that without having to parse the arguments?

I'm trying the following, but it's not working. I just simply want to pass the parameters, whatever they are and however many there are in the same order to the next method.
Ruby code:
def split(*args)
  self.to_s.split(args)
end

Splatting args turns a set of discrete args into an array. If you want to pass them to another method as args (and not an array) you need to unsplat them, thus:

Ruby code:
def split(*args)
  self.to_s.split(*args)
end

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