|
Jam2 posted:I've designed some simple Ruby objects to model a problem and I like the way they're composed. I'd like to use them as the backend for a Rails app and persist the data to a database. How can I integrate this design into rails without having to make these classes inherit from ActiveRecordBase and tangle them together with the RailsWay of doing associations? Is that really the way to go? Ideally, I'd like to integrate this in a way such that the objects, at most, have some other object that they persist themselves to. I don't want to uglify this with Rails code. HALP! You could mongoid it?
|
# ? Jan 18, 2014 16:51 |
|
|
# ? May 15, 2024 02:52 |
|
I'm hesitant to use MongoDB to persist such relational data. It seems better to use a RDBMS. Thoughts? I'm taking cues from Objects on Rails, but it hasn't become clear yet how to move forward.
|
# ? Jan 18, 2014 19:31 |
|
Jam2 posted:I've designed some simple Ruby objects to model a problem and I like the way they're composed. I'd like to use them as the backend for a Rails app and persist the data to a database. How can I integrate this design into rails without having to make these classes inherit from ActiveRecordBase and tangle them together with the RailsWay of doing associations? Is that really the way to go? Ideally, I'd like to integrate this in a way such that the objects, at most, have some other object that they persist themselves to. I don't want to uglify this with Rails code. HALP! So, for example, an Entity instance would automatically persist itself on entity.description = "lol inner database pattern"?
|
# ? Jan 18, 2014 21:14 |
|
Cocoa Crispies posted:So, for example, an Entity instance would automatically persist itself on entity.description = "lol inner database pattern"? I don't understand what you're saying to me.
|
# ? Jan 19, 2014 10:15 |
|
I'm still trying to make my own scaffold generator. For some reason, it cannot find active_record. I have no idea whats going wrong, I've been at this for a couple days. I monkey patched the scaffold_generator and it worked fine. Now I am trying to refactor that to a separate comand so we can scaffold for two different things (generate scaffold MyTest vs generate scaffold_dependency MyTest) Here are the classes that I copied and customized. These files are all located under lib/generators/* These files were all copied from the railties gem folder and then modified e.g. railties/lib/rails/generators/rails/* Ruby code:
Ruby code:
Ruby code:
Ruby code:
KoRMaK fucked around with this message at 17:24 on Jan 19, 2014 |
# ? Jan 19, 2014 17:21 |
|
Jam2 posted:I've designed some simple Ruby objects to model a problem and I like the way they're composed. I'd like to use them as the backend for a Rails app and persist the data to a database. How can I integrate this design into rails without having to make these classes inherit from ActiveRecordBase and tangle them together with the RailsWay of doing associations? Is that really the way to go? Ideally, I'd like to integrate this in a way such that the objects, at most, have some other object that they persist themselves to. I don't want to uglify this with Rails code. HALP! ActiveModel is a mechanism provided with modern Rails to allow you to hook various bits of Rails code (e.g. validations, naming) into your models as required without relying on ActiveRecord itself. You could investigate that as a mechanism to allow you to use your Ruby objects as the basis for forms and other Rails helpers, while doing the persistence yourself. That said, the interface and relationships you've already added to your code is almost identical to the way ActiveRecord operates, so if you're going to create something where the persistence is encapsulated within your models, you might as well use ActiveRecord. Otherwise, you could write a repository object capable of persisting and restoring your models. ActiveModel can help with the serialization if you like. Maybe more trouble than it's worth?
|
# ? Jan 19, 2014 21:11 |
|
Arachnamus posted:ActiveModel is a mechanism provided with modern Rails to allow you to hook various bits of Rails code (e.g. validations, naming) into your models as required without relying on ActiveRecord itself. You could investigate that as a mechanism to allow you to use your Ruby objects as the basis for forms and other Rails helpers, while doing the persistence yourself. Any examples of repository objects in the wild?
|
# ? Jan 20, 2014 08:26 |
|
Jam2 posted:Any examples of repository objects in the wild? Not many. There's a lot of talk around using a properly decoupled persistence layer in Rails but not a lot of walk. This guy seems to have had a decent stab at it: http://victorsavkin.com/post/41016739721/building-rich-domain-models-in-rails-separating and there's more on the theory in this article and the book / articles it references: http://devblog.avdi.org/2011/07/27/fowler-on-rails/ It was all the rage in 2009 and a couple of companies I know tried it out, but boy did it lead to a lot of extra work on the client dime, and was eventually scrapped. Rails has come a long way since then, so YMMV.
|
# ? Jan 20, 2014 08:53 |
|
Well, I've been trying "rake db:seeds" and pointing to a file to do initial population. I as having issues with the data and ASCII vs unicode characters, and solved most of those by converting them to their HTML equivalents. However, after resolving a few, I started getting a weird error: code:
My seeds.rb hasn't changed and looks like this: code:
|
# ? Jan 21, 2014 02:49 |
|
raej posted:Well, I've been trying "rake db:seeds" and pointing to a file to do initial population. I as having issues with the data and ASCII vs unicode characters, and solved most of those by converting them to their HTML equivalents. The `synchronize` call is part of the multithreading code, and might be unrelated to the actual cause. Not even sure why that would be cropping up when you're just creating some objects. Maybe your UTF-8 problems aren't entirely gone? Why are you loading the seed from a website, anyway?
|
# ? Jan 21, 2014 20:31 |
|
Arachnamus posted:The `synchronize` call is part of the multithreading code, and might be unrelated to the actual cause. Not even sure why that would be cropping up when you're just creating some objects. Maybe your UTF-8 problems aren't entirely gone? Thew tutorial had it set up from a website, so I figured why not. It also will help with testing since I'm running essentially dev locally on my machine, QA on one site, and production on the actual site. Same code will work on all 3 spots.
|
# ? Jan 21, 2014 22:18 |
|
raej posted:Thew tutorial had it set up from a website, so I figured why not. It also will help with testing since I'm running essentially dev locally on my machine, QA on one site, and production on the actual site. Same code will work on all 3 spots. Deleted branch, and copied the code above to the same spots, no error.
|
# ? Jan 22, 2014 01:37 |
|
raej posted:Deleted branch, and copied the code above to the same spots, no error. welp
|
# ? Jan 22, 2014 08:56 |
|
I want to have invitations users can send to other users to join two different models, should I create an invitation model and then bridge tables for the respective models? It seems simpler to just create model1_invitation and model2_invitation, but smarter to create the bridge tables, what is the best practice? Awesome, thanks for the help! VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV Newbsylberry fucked around with this message at 02:46 on Jan 24, 2014 |
# ? Jan 24, 2014 01:29 |
|
Newbsylberry posted:I want to have invitations users can send to other users to join two different models, should I create an invitation model and then bridge tables for the respective models? It seems simpler to just create model1_invitation and model2_invitation, but smarter to create the bridge tables, what is the best practice? Use a polymorphic association: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations Invitation belongs to invitable, and both of the invitable models have many invitations as invitable.
|
# ? Jan 24, 2014 02:19 |
|
Is there a way to force Open-URI to use UTF-8? It defaults to US-ASCII so imports fail every time there is a special character:Ruby code:
code:
|
# ? Jan 24, 2014 02:29 |
|
raej posted:Is there a way to force Open-URI to use UTF-8? It defaults to US-ASCII so imports fail every time there is a special character: If it is actually in UTF-8 you can tell Ruby what encoding a string is in using `String#force_encoding` e.g. `some_string.force_encoding("UTF-8")`. If you need to actually change the encoding a string is in that's a little more complicated.
|
# ? Jan 24, 2014 12:32 |
|
Kernel#open is mostly just an alias to IO::new, so the same options apply. Specify the encoding in the second parameter.Ruby code:
|
# ? Jan 24, 2014 15:29 |
|
Here's an example: @my_thing.persons.includes(:location).order("'persons.role' ASC, 'locations.name' ASC") The above doesn't work. The sql changes the column names via a "as" call and so the order by clause is effectively useless because person.role and locations.name aren't available. Uggghh it was a typo. I got too use to making things plural and person.role was typed as person.roles in the order clause. I'd like to make this the default scope for that relationship on the my_thing model. I tried this but it doesn't work Ruby code:
Ughhh 2: The has_many declaration works fine. Its the stupid caching we have that was screwing up the render. It didn't think there was any change so it kept showing me the old cached render. KoRMaK fucked around with this message at 17:24 on Jan 24, 2014 |
# ? Jan 24, 2014 17:02 |
|
Smol posted:Kernel#open is mostly just an alias to IO::new, so the same options apply. Specify the encoding in the second parameter. Didn't know about this flag. Cool.
|
# ? Jan 24, 2014 19:24 |
|
I am sorry to have to post with such a simple question but I've been internetting hard for about 12 hours and cant find a solution that works for me, which is crazy due to how simple of a problem this is. I have a hash in the form of |k, v| string=>int and I want to get the average of all the int values. That's it. My closest attempt is: hash.inject(0) {|total, (k, v)| total + v.last} But this is returning 'undefined method "last" for fixnum: (last integer value here)' Can anyone provide me with a quick way of averaging a set of int values? I find it incredibly frustrating that this isn't just a builtin method.
|
# ? Jan 28, 2014 17:20 |
|
Defghanistan posted:I am sorry to have to post with such a simple question but I've been internetting hard for about 12 hours and cant find a solution that works for me, which is crazy due to how simple of a problem this is. v is already the value you want. No need for v.last, just v
|
# ? Jan 28, 2014 17:24 |
|
If this is rails project or you're using active support anyway, use the following.Ruby code:
Ruby code:
Smol fucked around with this message at 17:33 on Jan 28, 2014 |
# ? Jan 28, 2014 17:27 |
|
Cool thanks for the replies guys, got it sorted!
|
# ? Jan 28, 2014 18:01 |
|
I have a download action in a controller that's giving me some trouble. The basic structure is this: code:
My problem is that, in some cases after a successful download, I want to refresh that calling screen to put a list of certain files at the bottom. I tried putting a redirect_to in the first if-block, but got an error that I can only use one render or redirect per action. I figured Rails just wasn't smart enough to realize that only one of them could be called, so I tried to move the existing redirect after the if-else-end block and fiddle with the parameters, but that also gave me the same multiple render/redirect error. So, I guess there must be an implicit render involved in the block with the send_to, but I don't know how to find, trace, or edit that. It doesn't seem to be using any actual view code, and rake routes shows me the route for getting into this download action, but I don't see anything that leads out of it to a view. I'm sure I'm missing something simple, but I'm not sure where to go from here.
|
# ? Jan 30, 2014 16:08 |
|
Peristalsis posted:I have a download action in a controller that's giving me some trouble. send_file is a 'renderer' here.
|
# ? Jan 30, 2014 18:01 |
|
The Journey Fraternity posted:send_file is a 'renderer' here. Bummer. Is there any way to edit or add to what it does? Or another way to force a refresh of the calling page?
|
# ? Jan 30, 2014 18:23 |
|
It's not really possible to know if a file download has started or succeeded in JavaScript. Your best bet is to initiate the download via hidden iframe that links to the zip download url, setting a cookie in the zip response and polling for the cookie in your JS code.
|
# ? Jan 30, 2014 18:26 |
|
Peristalsis posted:Bummer.
|
# ? Jan 30, 2014 18:29 |
|
Smol posted:It's not really possible to know if a file download has started or succeeded in JavaScript. Your best bet is to initiate the download via hidden iframe that links to the zip download url, setting a cookie in the zip response and polling for the cookie in your JS code. I don't think there's any JavaScript involved - this is all being done by http posts. I'm not worried at the moment about whether the download succeeds, I'm just trying to refresh the page that initiated the download after the send_file's render. Edit: KoRMaK posted:I swear I had to deal with something similiar but I kind of forget how. I think it involved using a javascript response. Don't send the file out via the first controller, but instead make the browser ajax request the file in your js response and then you can run a switch in the js response to see if it should do anything else. Ugh. This is all a bit beyond my expertise. Maybe I should just look for a way to send up a dialog or something with the file info for now. Peristalsis fucked around with this message at 18:36 on Jan 30, 2014 |
# ? Jan 30, 2014 18:30 |
|
Peristalsis posted:I don't think there's any JavaScript involved - this is all being done by http posts. I'm not worried at the moment about whether the download succeeds, I'm just trying to refresh the page that initiated the download after the send_file's render. If you just want to display something after a download, you'd do better by putting it into the page that kicks off the download. If you're wanting to only display something after a *successful* download, that's not really something you can do without an awful lot of faff.
|
# ? Jan 30, 2014 19:17 |
|
Arachnamus posted:If you just want to display something after a download, you'd do better by putting it into the page that kicks off the download. I want the former, but I don't have the information to display until the app starts processing the files to download. It's a list of files that are too large to download over the web app, and I get that list by looking at the file type of all the files selected by the user. Until the user selects the files and submits the list, I don't know which files can be downloaded. However, I could just mark all the non-downloadable files on the page with an asterisk and footnote to begin with, to let the user know that those files will just have their network locations/paths listed in a text file instead. Maybe my boss will go for that. If not, his last suggestion was to render a separate confirmation page that listed which files will be downloaded and which won't, and make the user submit from that form to start the actual download. I think that'll work, too, but it's a lot more hassle, and might not be much easier than just learning all this JavaScript stuff. Thanks for the ideas!
|
# ? Jan 30, 2014 19:51 |
|
Peristalsis posted:I want the former, but I don't have the information to display until the app starts processing the files to download. It's a list of files that are too large to download over the web app, and I get that list by looking at the file type of all the files selected by the user. Until the user selects the files and submits the list, I don't know which files can be downloaded. You could also tag the files which are known to be too large using a data attribute or a CSS class, then use javascript to build the in-page list of files that won't be downloaded as the user is selecting them.
|
# ? Jan 30, 2014 20:51 |
|
Peristalsis posted:I want the former, but I don't have the information to display until the app starts processing the files to download. It's a list of files that are too large to download over the web app, and I get that list by looking at the file type of all the files selected by the user. Until the user selects the files and submits the list, I don't know which files can be downloaded. You can also use a redirector on the confirmation page if you don't have an absolute need to submit a post. To do this, when they hit download, you would send them to a download page and then use JS to open the download link 5 seconds later. Send the files as 'attachment' and they'll never leave the confirmation page. If you need the post, consider instead creating a temporary download object that holds which files you need to send, so you will have something to redirect to.
|
# ? Jan 30, 2014 21:48 |
|
Arachnamus posted:You could also tag the files which are known to be too large using a data attribute or a CSS class, then use javascript to build the in-page list of files that won't be downloaded as the user is selecting them. kayakyakr posted:You can also use a redirector on the confirmation page if you don't have an absolute need to submit a post. To do this, when they hit download, you would send them to a download page and then use JS to open the download link 5 seconds later. Send the files as 'attachment' and they'll never leave the confirmation page. Thanks for the additional ideas. I don't care about posting or not posting, I'm just trying to get a fix out for this as quickly as possible (i.e. changing as little of the code and structure as I can at this point). For the next version, we'll want something better than my footnote though (my boss went for that idea, so I'm on to the next artificially induced crisis), so I'll try to look into integrating JavaScript and whatnot. I've been able to work primarily on the back end of this app, and now that I'm getting UI stuff thrown at me, I need to figure out what the hell I'm doing with HTML, css, and JavaScript.
|
# ? Jan 31, 2014 14:39 |
Anyone have experience with Carrierwave uploads into an accepts_nested_attributes_for object? The child object is created just fine with the other properties. But the upload part just won't take.
|
|
# ? Feb 1, 2014 01:09 |
|
These are code blocks right? (the failure/succes.html{} bit)Ruby code:
I'm using inherited_resources. I've come accross a situation where both my .js and .html responses should be the same. How do I combine them? Ruby code:
Ruby code:
Ruby code:
|
# ? Feb 3, 2014 16:21 |
|
try: Ruby code:
|
# ? Feb 3, 2014 17:36 |
|
kayakyakr posted:try: edit: oh duh, the help page does inform me of .all http://apidock.com/rails/ActionController/MimeResponds/Collector e: Found it down the page here http://edgeapi.rubyonrails.org/classes/ActionController/MimeResponds.html Ruby code:
Ruby code:
KoRMaK fucked around with this message at 17:54 on Feb 3, 2014 |
# ? Feb 3, 2014 17:42 |
|
|
# ? May 15, 2024 02:52 |
|
KoRMaK posted:
I also believe that it falls through (or fails to do so). So if you have success.json on the first line and then success.all on the second line, it'll respond with the json line and not the all line. Don't quote me on that, it might cause a double render as well.
|
# ? Feb 3, 2014 20:29 |