|
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 21:39 on May 8, 2013 |
# ? May 8, 2013 18:48 |
|
|
# ? May 15, 2024 10:39 |
|
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 23: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 9, 2013 00: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 20: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 20: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 20: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 20:43 |
|
Are you trying to output CSV?
|
# ? May 15, 2013 20: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 20: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 21:23 on May 15, 2013 |
# ? May 15, 2013 21: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 21: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 04: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 04: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 17:45 |
|
Ruby code:
|
# ? May 16, 2013 18:01 |
Pardot posted:
Cool, I've never seen this before. Thanks!
|
|
# ? May 16, 2013 18:56 |
|
Pardot is obviously the master but I'd like to try my hand at writing that with AR:code:
|
# ? May 17, 2013 00: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 03:46 |
|
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 16:43 |
|
In ruby, is there an easy way to tell at the point of execution whether "self" refers to a class or an instance? I'm debugging some meta-programming hell (that I didn't write) and self.extend is getting called on a class instead of an instance somehow, and causing class-wide definitions that I don't want to exist. Basically, I want the opportunity to insert in "debugger if self is a class" so I can see where it's being called from. Any thoughts?
|
# ? May 20, 2013 17:43 |
|
Lexicon posted:In ruby, is there an easy way to tell at the point of execution whether "self" refers to a class or an instance? I'm debugging some meta-programming hell (that I didn't write) and self.extend is getting called on a class instead of an instance somehow, and causing class-wide definitions that I don't want to exist. Basically, I want the opportunity to insert in "debugger if self is a class" so I can see where it's being called from. Any thoughts? Doesn't the class method tell you want you need to know? code:
|
# ? May 20, 2013 18:57 |
|
I know this is a really stupid question, but what the heck. I'm working with push notifications in my iOS app. Each notification needs its own unique identifier. The notifications are very ephemeral. They're created and pushed to a remote server (Urban Airship) from my Rails server in one step, so I don't need to store a notification object on my Rails server. I just need to assign the notifications a unique ID when they're created just before they're pushed. My thought was to just create a Notification model and give it no attributes. Then, every time the Rails app creates a new notification to push, it will also create and save a new Notification instance, which will auto-increment its id and assign the object's id to the notification's unique identifier (UA calls these aliases). This sounds like a dumb way to create a counter. If my app is successful, it's going to fill my database with tens of thousands of empty records over the lifetime of the app. Is there a cleaner, more pragmatic way to create a counter like this that also avoids race conditions?
|
# ? May 21, 2013 06:51 |
|
plasticbugs posted:I know this is a really stupid question, but what the heck. https://rubygems.org/gems/uuid
|
# ? May 21, 2013 07:31 |
|
Yeah I'd go the UUID route if you can. If you reallllllly need incrementing ones you can just create a raw sequence. http://www.postgresql.org/docs/9.2/static/functions-sequence.htmlcode:
Ruby code:
Pardot fucked around with this message at 07:36 on May 21, 2013 |
# ? May 21, 2013 07:33 |
|
Pardot posted:Yeah I'd go the UUID route if you can. I didn't even know this was a thing. Thanks guys!
|
# ? May 21, 2013 09:47 |
|
It was added in 1.9.
|
# ? May 21, 2013 09:51 |
|
Lexicon posted:In ruby, is there an easy way to tell at the point of execution whether "self" refers to a class or an instance? self.is_a? Class returns true if self is an instance of Class. Ruby code:
|
# ? May 22, 2013 15:08 |
|
Cocoa Crispies posted:self.is_a? Class returns true if self is an instance of Class. Perfect, thanks!
|
# ? May 22, 2013 15:18 |
|
In reference to my cellar list above. How would I go about destroying a Cellared_Beer with the correct variables (size, year) when this is being viewed in the User model? For instance, the view attached is users/1/cellar I need the "delete" link to destroy a cellared_beer (which has user_id, beer_id, size, year, created_at, updated_at) that matches that line item (unique on beer_id, size, year)
|
# ? May 22, 2013 20:19 |
|
raej posted:In reference to my cellar list above. How would I go about destroying a Cellared_Beer with the correct variables (size, year) when this is being viewed in the User model?
|
# ? May 22, 2013 20:23 |
|
Misogynist posted:Using the approach you've chosen, you're overcomplicating things and making your schema really difficult to work with -- if you decide to modify your constraints anywhere in the system, you have lots of things you need to update because of the incredibly tight coupling you've chosen. If I were you, I'd create a single numeric primary key for each combination of (beer_id, size, year) and use simple unique constraints instead of a compound primary key to ensure uniqueness properly. Well, each cellared_beer has a unique key... The problem I was having was grouping like beers for display in a cellar. If someone adds a 6-pack of something, I'd rather not have 6 lines for that beer. But I need to be able to decriment that amount when someone decides to drink one of those beers.
|
# ? May 22, 2013 20:33 |
|
raej posted:Well, each cellared_beer has a unique key... The problem I was having was grouping like beers for display in a cellar. If someone adds a 6-pack of something, I'd rather not have 6 lines for that beer. But I need to be able to decriment that amount when someone decides to drink one of those beers. On your CellaredBeer model, add a #drink method that decrements the quantity and updates drank_at. If the quantity is zero, keep it around so the user doesn't have to rack their brain over what that one beer they had last month was.
|
# ? May 22, 2013 22:28 |
|
Cocoa Crispies posted:On your CellaredBeer model, add a #drink method that decrements the quantity and updates drank_at. If the quantity is zero, keep it around so the user doesn't have to rack their brain over what that one beer they had last month was. So would it be better to style cellared_beers as id, user_id, beer_id, year, size, amount? Then be able to lower amount every time a beer is removed? How would that work when say someone buys a 6 pack, then a week later, buys another 6 pack and hasn't drank any? Wouldn't that be 2 lines of 6?
|
# ? May 22, 2013 23:03 |
|
I think you need to zoom out a little. What exactly are you trying to measure? Are you trying to measure number of bottles or number of packages? If you had a 6 pack and a 12 pack, what do YOU want to display? 18, 1/6 and 1/12, or both of those options? That'll change how you model it, and right now I'm not exactly clear on which you'd like.
|
# ? May 22, 2013 23:26 |
|
Lexicon posted:In ruby, is there an easy way to tell at the point of execution whether "self" refers to a class or an instance? I'm debugging some meta-programming hell (that I didn't write) and self.extend is getting called on a class instead of an instance somehow, and causing class-wide definitions that I don't want to exist. Basically, I want the opportunity to insert in "debugger if self is a class" so I can see where it's being called from. Any thoughts? Pedantry: everything in Ruby is an instance. Fixnum.is_a?(Class) is true not because Fixnum is a class per se, but because Fixnum is literally an instance of Class.
|
# ? May 22, 2013 23:36 |
|
Nybble posted:I think you need to zoom out a little. What exactly are you trying to measure? Are you trying to measure number of bottles or number of packages? If you had a 6 pack and a 12 pack, what do YOU want to display? 18, 1/6 and 1/12, or both of those options? I want to keep track of the number of like bottles based on beer, year, and size. The 6-pack analogy was just knowledge that most 12oz bottles come in a 6-pack, and a person would be adding many of a like bottle based on that.
|
# ? May 23, 2013 00:25 |
|
raej posted:So would it be better to style cellared_beers as id, user_id, beer_id, year, size, amount? Then be able to lower amount every time a beer is removed? Add a Beer#buy(user, quantity) or User#buy(beer, quantity) method that handles the finding/creating and incrementing. CellaredBeer might belongs_to :user, belongs_to :beer, and have a quantity, size/distribution (i.e. 12oz can, 12oz bottle, 40oz can), bought_at, drank_at, etc.
|
# ? May 23, 2013 01:20 |
|
xtal posted:Pedantry: everything in Ruby is an instance. Fixnum.is_a?(Class) is true not because Fixnum is a class per se, but because Fixnum is literally an instance of Class. As a fellow pedant, thanks for putting me straight
|
# ? May 23, 2013 04:14 |
|
Cocoa Crispies posted:Add a Beer#buy(user, quantity) or User#buy(beer, quantity) method that handles the finding/creating and incrementing. Can you enlighten me as to what this is called so that I can read up on it more? This sounds like the ticket!
|
# ? May 23, 2013 04:19 |
|
|
# ? May 15, 2024 10:39 |
|
raej posted:Can you enlighten me as to what this is called so that I can read up on it more? This sounds like the ticket! It's called object oriented design: you put methods on objects that match your domain so instead of computer bullshit they're a domain model.
|
# ? May 23, 2013 17:38 |