|
I have a list of beers that a user has in their cellar through cellared_beers so that a user has many beers through cellared_beers and vice versa. Each cellared_beer has extra data like year and size attached. I am wanting to display the tallied results of the cellared_beers for a user on a page. Right now I have the following code which will get the counts: code:code:Something like: Foo Beer(beer.name), Bar Brewery(beer.brewery.name),2008,12oz,1 Foo Beer(beer.name), Bar Brewery(beer.brewery.name),2009,12oz,3 Foo Beer(beer.name), Bar Brewery(beer.brewery.name),2010,12oz,1 EDIT: Doing this in my controller: code:code:Something like: code:raej fucked around with this message at May 8, 2013 around 20:39 |
| # ? May 8, 2013 17:48 |
|
|
| # ? May 19, 2013 19:36 |
|
DreadCthulhu posted:So by default the cookies are signed and basically given full trust regarding whether the user is who he claims he is? Permissions in there as well? Yes. Signing them isn't a magic solution, but it prevents anyone from actually tampering with a cookie to change their user_id or role, or whatever you happen to store in a cookie, as long as no one knows what your session secret is ( 'config/initializers/secret_token.rb' ), as long as OpenSSL's HMAC remains trusted.
|
| # ? May 8, 2013 22:10 |
|
UxP posted:Yes. Signing them isn't a magic solution, but it prevents anyone from actually tampering with a cookie to change their user_id or role, or whatever you happen to store in a cookie, as long as no one knows what your session secret is ( 'config/initializers/secret_token.rb' ), as long as OpenSSL's HMAC remains trusted. Fair enough. I remember reading that the rationale is that if someone steals your secret token straight off your production box, you're probably screwed enough that the cookie is the last of your problems. As in, they likely have full access to the DB at that point, which is a lot more interesting. If I'm understanding this correctly, the advantage of the signed cookie approach is that you don't have to have a server-side map of permissions/sessions, and you can effectively "distribute" the burden onto the clients? Keeping a properly synced user/permissions/sessions map in memory at all times and persisting it to db seems like extra work. I think some people use solutions like Redis for that as well.
|
| # ? May 8, 2013 23:05 |
|
Is there a way to break up a hash into an array? That should solve my previous problem. controller code:code:Is there a way to spit it out so that I can call beer[1] and get "Test beer" instead of ","?
|
| # ? May 15, 2013 19:07 |
|
raej posted:Is there a way to break up a hash into an array? That should solve my previous problem. code:
|
| # ? May 15, 2013 19:18 |
|
Lexicon posted:
Hmm, maybe what I have ins't a hash at all then? When I'm assembling the data in the controller, I need it to be broken up instead of one big string. Any ideas?
|
| # ? May 15, 2013 19:33 |
|
raej posted:Is there a way to break up a hash into an array? That should solve my previous problem. The .each.map looks suspect. Just use map? I don't see why you'd want to chain .each.map like that.
|
| # ? May 15, 2013 19:43 |
|
Are you trying to output CSV?
|
| # ? May 15, 2013 19:44 |
|
manero posted:The .each.map looks suspect. Just use map? I don't see why you'd want to chain .each.map like that. Changing it to just .map still spits out a big string Smol posted:Are you trying to output CSV? I'm not trying to output csv. Ideally in my view I would call up each value to the view fir display purposes so that code:
|
| # ? May 15, 2013 19:51 |
|
Ok, I think I understand your problem now. Does this do what you're looking for?code:Smol fucked around with this message at May 15, 2013 around 20:23 |
| # ? May 15, 2013 20:02 |
|
Smol posted:Ok, I think I understand your problem now. Does this do what you're looking for? Woo! Yes, that's exactly what I was looking for! Thank you!
|
| # ? May 15, 2013 20:40 |
|
Isn't that going to do n+1 queries where n is how many beers they have? Honestly, I'd just write a single raw sql query and skip active record entirely at this point. However I've been working on a postgres service for a few years now, so I might be crazy.
|
| # ? May 16, 2013 03:05 |
|
What is it that you're actually trying to do? Can you give me an example of the input that you've got and the output that you desire? Because yes, that's going to do n+1 queries and get awful slow.
|
| # ? May 16, 2013 03:41 |
|
prom candy posted:What is it that you're actually trying to do? Can you give me an example of the input that you've got and the output that you desire? Because yes, that's going to do n+1 queries and get awful slow. Models are Users, Beers, Breweries, and Cellared_beers. Breweries have many beers. Users have many beers through cellared_beers. The Beer model contains information on a kind of beer (Name, brewery, description, ABV, style, etc) A Cellared_beer represents a physical bottle one owns and the attributes. (Size, year, etc) A user can go to a beer page and "add to cellar" which asks for year and size of the beer to be added. This creates a cellared_beer with user_id, beer_id, year, size. A user can go to his cellar and list all of the cellared_beers, but grouped by similar size and dates. Output would look like: code:
|
| # ? May 16, 2013 16:45 |
|
Ruby code:
|
| # ? May 16, 2013 17:01 |
|
Pardot posted:
Cool, I've never seen this before. Thanks!
|
| # ? May 16, 2013 17:56 |
|
Pardot is obviously the master but I'd like to try my hand at writing that with AR:code:
|
| # ? May 16, 2013 23:42 |
|
prom candy posted:Pardot is obviously the master but I'd like to try my hand at writing that with AR: Wrap it all up in a class method!
|
| # ? May 17, 2013 02:46 |
|
|
| # ? May 19, 2013 19:36 |
|
DreadCthulhu posted:If I'm understanding this correctly, the advantage of the signed cookie approach is that you don't have to have a server-side map of permissions/sessions, and you can effectively "distribute" the burden onto the clients? Keeping a properly synced user/permissions/sessions map in memory at all times and persisting it to db seems like extra work. I think some people use solutions like Redis for that as well. HMAC is secure when you pick a quality secret. Rack's cookie session implementation that Rails uses SHA-1, which while it's too weak for passwords, are fine for data that isn't a human-generated (i.e. bad) password. My understanding is that the cookie is just identity, and authorization for actions based on that identity lives in the app at runtime. Syncing a user/session map between memory and database isn't hard, but expiring it can be, and if your app lives in multiple datacenters, that adds a whole new dimension of "fun" to database synchronization.
|
| # ? May 17, 2013 15:43 |













