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
fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
I'm fairly new to Rails and I'm trying to use it to make a web-based wargame. I was building a database to represent all of the countries (board spaces if this were a real game) with columns for each of the things a country has - player1 troops, player2 troops, and so on, with the idea that playing the game would be the machine updating each of those columns as the board state changes.

However, I realized as I started doing it that this would mean the database would be constantly changing, so there couldn't be multiple games. So now I'm thinking that each game would begin with creating a new database "foo" from template "bar" where foo is an automatically sequencing number(ie, game000001, game000002, etc) and bar is the original database. However, this is a little out of my comfort zone and I don't know if that's a good idea or if there's a better way, and Google isn't turning up anything helpful.

Is this a sane way to approach the problem? (Obviously I would need to have the server clean itself up periodically since I don't want scads of completed/zombie game databases laying around.)

Adbot
ADBOT LOVES YOU

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

kayakyakr posted:

That's a horrible way to design this. Horrible.

Create a Game table. Link that to a Competitor table. Have a Pieces or Troops or whatever table that tracks what forces the competitor has and where they are located. Use indexes to keep it all fast.

Game has many competitors
Competitors has many troops
etc etc.


e: Or just do it all in a document database where each game is one whole document which would work if the size was limited. Could do this with postgres and json fields too.

Thanks, that's helpful. I got hung up thinking that there should be a Countries table that keeps track of who has what where, but thinking about it in the way you suggest makes a lot of sense.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
I have a weird situation with ActiveRecord associations, and since Rails is fighting me tooth and nail on what I'm trying to do, I get the sense I might be approaching it the wrong way. (Class names have been changed to protect the innocent.)

I have a chain of has_many through, like so:

code:
class Industry
	has_many :companies
	has_many :employees through: :companies
	has_many :dogs through: :employees
end

class Company
	belongs_to :industry
	has_many :employees
	has_many :dogs through: employees
end

class Employee
	belongs_to :company
	has_many :dogs
end

class Dog
	belongs_to :employee
end
Industry has a property 'name' which is just a string, like widgets. Dog has a bunch of properties like breed, color, is_fuzzy, and so on.

What I want to do is have a method on Dog that finds all of the dogs that match the parameters passed in, all of which are optional. One of those parameters can be an industry name, so the method could find all is_fuzzy poodles where the related industry is widgets.

I approached making a search that can find various Dogs like this:

code:
class Dog < ActiveRecord::Base
  def self.filter(attributes)
    attributes.inject(self) do |scope, (key, value)|
      return scope if value.blank?
      case key.to_sym
      when :id, :breed 
        scope.where(key => value)
      when :is_fuzzy
	scope.where("is_fuzzy = ?", is_fuzzy)
      when :min_age
        scope.where("age >= ?", min_age)
      when :max_age
	scope.where("age <= ?", max_age)
      else
        scope
      end 
    end  
  end
end
However, for the life of me I can't figure out how to also implement the parameter from the Industry class. Since it's not a part of the Dog database I can't just query for it with a .where. Is using scopes the wrong approach, and, if so, what would work better?

fantastic in plastic fucked around with this message at 02:26 on Aug 27, 2014

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

KoRMaK posted:

You mean they are in different Database or different tables?

This is the kind of thing that would get handled with a join.

They're all different tables in the same database, sorry.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

Arachnamus posted:

Join ye a table:

Thanks, this was just what I needed to get myself unstuck. :)

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
Some poor idiot will actually pay for that? Clearly I'm in the wrong business and should stop everything, make a clone of RubyMonk that includes "competence tests" and charge people a couple hundred bucks to get the Tao Jones Ruby Institute Silver Star or whatever.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

KoRMaK posted:

Thanks, but I'm not sure this will apply to my situation. Doing current_account.users builds a query that is "select * from users where account_id = 1" (1 being the id of current_account).

So I want to do current_account.users.my_function and inside of my function I'd like to pluck out the account_id being used. I'd also like to just in general know if there is some meta properties I can inspect to get all the variables currently being passed in to the query maker.

I'm not sure I understand the question.

Ruby code:
current_account.id
should return the id of current_account, assuming that current_account is set. This should match the account_id in the related users, which I think is what you're asking for?

Ruby code:
current_account.users.first.account_id
should return the account_id of the first user in the response from current_account, assuming it's set somewhere. Since it's a has_many and I assume account_id is the key that sets up the associations, they should all be the same.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
What happens if Alice puts Bob's email address in the "user fills in the email address of the old account" step?

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

kayakyakr posted:

Seems like Bob would get an email asking him to claim his account?

That's fairly close to a reset password flow, which is fairly standard.

Ah, okay. From the way I was reading the flow, it seemed like there'd be an AccountClaim with Alice's user_id and Bob's email address, which might be dangerous if Alice is evil and tricks Bob.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
Tangential to the testing conversation - is there a best practice for a situation like when your client has an API, you're building a new application that consumes that API, but the client's documentation is poo poo and the API often doesn't behave as specified?

It's not exactly the same case as we're discussing here, but a company I've worked with has tended to use tests touching the API to persuade intransigent clients that their API is, in fact, not behaving to specification. It tends to drive developers crazy because it can be hard to tell if the test is badly written or the API is wrong.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
If you plan your career well, you can be long gone before any of the lovely technical decisions and kludges you made to get version 1 working ever come back to bite you.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

So, if I'm understanding your description, you've got one AJAX call that's something like GET api/whatevers?q=foo that you fire off when a user selects an option in a select box. The API does whatever it does to handle that request and sends you the results, which you're setting to a variable and then using that variable for other code, which you theorize is executing before your variable is set.

You could try sticking your code that needs the variable into something like this:

code:
function awaitResponse() {
    if(typeof @yourVariable !== "undefined") {
        //do the thing here
    }
    else {
        setTimeout(awaitResponse, 250);
    }
}
That would check the variable every 250ms and if it's set to something, execute your code, otherwise wait another 250ms. There's probably a better way to do it, but I think that would at least help you prove your async hypothesis.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
code:
User.joins(:books).group("users.id").having("count(books.id) > 4").length
should do it.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

Waroduce posted:

annnnd I'm not really sure where to go from here....and I don't really get hashes but i know I need something like

if part that y or n == “y”

users << part that is cd/pt/etc (can use gsub(“ Staff”, “”) to get just CD)
end
end

if it has staff it goes under staff, review goes to review. I'm not sure how to like sort that out or code it in, but I can use gsub to remove the "STAFF" portion of the header and than that abbreviation is what appears on our webpage handling user permissions with a checkbox i check on or off to give permission or not.

so..i dunno what to do. I feel like one of those monkeys making GBS threads on a typewriter who pooped out 2 sentences to Shakespeare

As he lay dying, the last living descendant of the last Aztec high priest whispered:

code:
my_hash = hashes[0]

my_hash.each do |key, value|
  puts key
  puts value
end
If I'm understanding your explanation correctly, I think you've got the right big-picture idea on ways to proceed. The next step is to figure out how to get all of the hashes with a value of "y", then you'll have to figure out some way to parse the keys for their abbreviation and "staffiness"/"reviewiness".

Then you'll have to figure out how to do whatever the end goal for the program is, which I guess is go to a webpage and click particular checkboxes to autofill a form?

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

Waroduce posted:

Can you explain your code to me in plain English and how it fits into what i've got?

Sure. A hash like

code:
{ "name" => "Title", "CD Staff" => "y", "PT Staff" => "y", "CD Review" => "n", "PT Review" => "n" }
has a bunch of elements, each composed of a "key" and a "value". The key is the bit before the => and the value is the bit after it.

My snippet should let you loop through all of the elements in a hash and print the key and value for each. My intention in posting it was to try to demonstrate that you'd need to read each element and that you can work with the keys and values individually. I apologize if the way I went about it was too opaque; I thought you were a junior dev rather than someone trying to write something for the first time. :)

If you play around with the snippet, you should be able to construct an if-statement which will shovel all of the "y" values into another array of hashes rather than calling puts (which just displays them to the screen).

Then you could do something like loop over that new array, split your keys into, eg, ["PT", "Staff"] and then have some if-statements that do something with that to record which checkboxes need to be checked.

Then you'd have a scraper or something navigate to your website and check things appropriately.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

Waroduce posted:

I have soccer tonight so will look at this later or tomorrow, but I'm not using ruby by choice, it's what we use to run all of our scripts in my department so yeah

Also, I used to be in sales (forgive me) for enterprise content management systems and I rubbed elbows with a lot of CIO/CTO decision maker types who always told me ruby on rails is like the next big thing and there's gunna be a bunch of jobs and they'll pay really well and have some longevity cause it's a rare language both in professional settings at like conventions and poo poo and in more personal settings like cigar bars. I heard this from several fortune 100 companies.

This was like ~1.5-2 years ago so you guys would probably be in a position to tell me if that's actually true

Ruby on Rails was big 4 years ago. It lets you do a really specific thing (basic CRUD web app/api) really easily if you're willing to adopt the Rails Way To Do Things. A fair amount of consultantware/internal tools/random web APIs are built in it, but of all of the modern web stacks it's among the worst for performance.

It's an unusual framework to know because it isn't, to my knowledge, taught in CS programs. It became popular with Silicon Valley startups and consulting firms. Most of the hardcore Ruby people I know are either self-taught or picked it up by chance on some consulting contract.

Adbot
ADBOT LOVES YOU

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

xtal posted:

Just use minitest like our lord dhh gave you. Rails is omakase, and all

I think my company's CTO unironically holds this position.

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