|
Are you trying to create the base, db backed object as "Record", and then namespace it's children under it, like this?Ruby code:
You'd have better luck defining a base class inside a module and inheriting all your children from that. Ruby code:
UxP fucked around with this message at 00:20 on Apr 13, 2013 |
# ? Apr 13, 2013 00:18 |
|
|
# ? May 15, 2024 19:00 |
|
I've got an ActiveRecord model with an metadata field that I'd like to be a hash in ruby-land, but be serialized to the database as JSON. Ideally, this conversion would all be handled nicely by before_save hooks and so forth. What's the best way to go about this?
|
# ? Apr 22, 2013 03:12 |
|
Lexicon posted:I've got an ActiveRecord model with an metadata field that I'd like to be a hash in ruby-land, but be serialized to the database as JSON. Ideally, this conversion would all be handled nicely by before_save hooks and so forth. Depends on which version of ActiveRecord you're using, but you should be able to do this with >= 3.1 Ruby code:
|
# ? Apr 22, 2013 04:03 |
|
^^ Perfect, thanks! Edit: What about making the attribute a HashWithIndifferentAccess on deserialize, not just a regular Hash? Lexicon fucked around with this message at 18:19 on Apr 22, 2013 |
# ? Apr 22, 2013 15:46 |
|
Is this thread appropriate for Ruby questions that aren't focused on Rails? If so: I'm new to Ruby, and as a learning project I'd like to do something with Rack. Ruby seems to have a nice ecosystem for infrastructure-as-code, but I'm not sure what is the most commonly-used web server configuration for folks using Rack but not Rails. Do folks usually start up servers using Thin and then tie them in to nginx using sockets, or...? I'm curious what most folks do (assuming they're self-hosting, or at least not using Heroku).
|
# ? Apr 23, 2013 04:40 |
|
MariusMcG posted:Is this thread appropriate for Ruby questions that aren't focused on Rails? If your core question is "how should I host my Rack application?" your first answer should be "whatever's easiest" and your second answer should be "whatever will help scale my app to where it needs to be" after the the first answer stops being good enough. Thin and Unicorn are the two most popular Rack servers these days, and the dichotomy generally breaks down as follows: if you want something that's really easy to get working, use Thin, and if you need to squeeze every drop of performance out of your application, use (and hand-tune) Unicorn. Passenger is still a good option if you have some application that mostly sits idle and can be spun up on demand the first time a user hits it. For internal applications where you're not dealing with high request throughput, if you wanted to keep configurations really simple you could probably get away with just running the app server and not front-ending it with something like Nginx at all. A frontend web server will give you the following benefits, though:
You have the option of using Unix sockets or proxying to the app server over TCP. In synthetic benchmarks, Unix sockets are faster, but in practice, the performance difference is generally negligible unless you're operating at insane scale.
|
# ? Apr 23, 2013 05:49 |
|
Thank you very much! That's a very useful answer.
|
# ? Apr 23, 2013 06:13 |
|
How do I specifiy the namespace (not sure what to call it in ruby or rails, I'm using it as if this were java or C#) of a variable I am using? I have a local variable that is getting in the way of a similar one in a module. Is there a way I can delineate between the two? I have had the same problem with a definition in the application helper override a model's scope definition. Similar problem, I have overridden I18n.t in my application helper but when I try to call it from the application helper file it uses the default I18n.t version instead of the one I wrote. Doing ApplicationHelper.t gets me the right one, but it loses the context of my current_account variable which I need t to be aware of. KoRMaK fucked around with this message at 16:56 on Apr 23, 2013 |
# ? Apr 23, 2013 15:02 |
|
Having an issue with a form_for on a routes resource and Rails' automatic pluralization. I have a definition in my routes.rb that goes like this:pre:resources :magic_fives pre:form_for @magic_five ...etc I've researched this a bit already, and I see it's a consistent bug in Rails 3 regarding route :resource definitions automagically being pluralized. What I'm curious about is the most efficient way of addressing it. Has anyone had to deal with this before?
|
# ? Apr 25, 2013 16:09 |
|
mmachine posted:Having an issue with a form_for on a routes resource and Rails' automatic pluralization. I have a definition in my routes.rb that goes like this: Try tossing this in an initializer: code:
|
# ? Apr 25, 2013 16:25 |
|
The Journey Fraternity posted:Try tossing this in an initializer: Oh very nice -- thanks! Exact type of solution I was looking for.
|
# ? Apr 25, 2013 17:19 |
|
Ok, more fun with the magic_five model. This might not be the most graceful relationship, but in this magic_five model there are five fields, each of which points to a record of the same TYPE of model, but not the same model. So it's like: magic_five.potion_1 magic_five.potion_2 magic_five.potion_3 magic_five.potion_4 magic_five.potion_5 ...but each of the potion_% fields points to a record in my potions table. What I'd like to (try) to do is define my magic_five model to know that each of those fields is a corresponding potion record, so I could hit it like: magic_five.potion_1.potion_name So far these relationships have been drawn explicitly with manual querying -- now I'm at a point where it could be helpful to give them a more robust relationship in my magic_five.rb model. The first attempt I gave at this looked something like: has_many :potions, :primary_key => :potion_1, :foreign_key => :id Which doesn't work, but I'm not sure what piece I'm missing.
|
# ? Apr 28, 2013 16:03 |
|
With the way you've got it set up you would use something like:code:
code:
|
# ? Apr 28, 2013 17:59 |
|
prom candy posted:With the way you've got it set up you would use something like: Very nice -- right now I went with the first solution. Second solution definitely feels like the right one, though. Actually a lot of our models for this app use mapping tables like you describe already -- I think we wrote this specific feature off as a hard five cap, so probably worth refactoring on the next pass.
|
# ? Apr 29, 2013 15:00 |
|
Anyone have any insight to share on how to structure view specific javascript files? I have "document.ready" code for specific views that I want to fire *only* during those view executions...
|
# ? Apr 29, 2013 21:14 |
|
Lexicon posted:Anyone have any insight to share on how to structure view specific javascript files? I have "document.ready" code for specific views that I want to fire *only* during those view executions... There are a couple approaches. One is to include a guard statement in your document.ready code to check to see if the correct elements are there (i.e. you are on the correct page for your code) IMO a better way to do it is to break the page-specific code into a separate file, and only include that JS on the pages that need it: app/views/layouts/application.html.erb: code:
code:
I tend to split the JS files between the definition of the code (e.g. a coffeescript class), and the creation/usage of it. This lets you still precompile posts_index.js from your application.js, and then do something like this on your page: code:
|
# ? Apr 29, 2013 22:43 |
|
Use something like https://github.com/thoughtbot/flutie/blob/master/lib/flutie/page_title_helper.rb to put a controller & action class on your body tag, and guard your JS files with that. if you're using jQuery, you can use http://api.jquery.com/jQuery/#jQuery3 instead of document.ready too.
|
# ? Apr 29, 2013 22:57 |
|
I'm trying to run a single migrate an am running into a problem. I was wondering if anyone could help me.code:
I do require 'db/migrate/20130415224445_create_plan_item_updates.rb' Then I do PlanItemUpdate.up and the error I see is: code:
uhg im an idiot. Its CreatePlanItemUpdates.up PastaSky fucked around with this message at 02:05 on Apr 30, 2013 |
# ? Apr 30, 2013 01:51 |
|
Lexicon posted:Anyone have any insight to share on how to structure view specific javascript files? I have "document.ready" code for specific views that I want to fire *only* during those view executions... http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution I love this system.
|
# ? Apr 30, 2013 05:47 |
|
manero posted:js stuff Thanks Manero, Cocoa Crispies, PastaSky, prom candy; that should be enough of a lead to get me started.
|
# ? Apr 30, 2013 15:25 |
|
My app has a "feed" of sorts where a user logs in and a bunch of stuff is displayed similar to a Facebook feed. Each item on the feed has a static URL associated with it, and that URL is obviously available in the view. I would like a user to be able to bookmark that static URL by clicking a link. I have a bookmark model that belongs_to user and a user model that has_many bookmarks. But I can't wrap my head around how to make a single URL that the user can click that will create an entry in the bookmark table. Does this make sense?
|
# ? May 2, 2013 03:11 |
|
Either nest BookmarksController under Users in the routes file and POST to /users/foo/bookmarks (BookmarksController#create) or grab the user id from your session variable and POST to /bookmarks.
|
# ? May 2, 2013 07:36 |
|
Question for you Rails experts: how does Rails decide what environment the code is running as? Is ruby reaching out to the shell and checking that a certain environment variable is set correctly? On that note, do you know how the web app decides what database to talk to when it receives a call? For example, say I'm running functional tests that hit Postgres. Does every single ActiveRecord call check the environment variable? Does the test suite just reset it back to what it was before once it's done?
|
# ? May 3, 2013 22:53 |
|
DreadCthulhu posted:Question for you Rails experts: how does Rails decide what environment the code is running as? Is ruby reaching out to the shell and checking that a certain environment variable is set correctly? Kind of, yeah: https://github.com/rails/rails/blob/master/railties/lib/rails.rb#L56 ActiveRecord memoizes the environment so it won't actually call out to find the environment on every request. Ruby itself will populate the ENV hash with your shell's environment variables. Start up an irb session and call that magic object, it should be identical to calling env from bash or zsh or whatever.
|
# ? May 4, 2013 02:31 |
|
DreadCthulhu posted:On that note, do you know how the web app decides what database to talk to when it receives a call? For example, say I'm running functional tests that hit Postgres. Does every single ActiveRecord call check the environment variable? Does the test suite just reset it back to what it was before once it's done? To be a bit more specific, ActiveRecord establishes connections when the Rails environment starts, so usually once per test run, if that makes sense. It simply connects to the environment specified in RAILS_ENV, as specified in your database.yml, which then is reflected as Rails.env in your running app.
|
# ? May 4, 2013 03:40 |
|
UxP posted:Kind of, yeah: https://github.com/rails/rails/blob/master/railties/lib/rails.rb#L56 First of all, ||= is brilliant, glad you pointed that out. I don't write much Ruby these days, and so I had to immediately check if JS happened to have that operator, since I abuse the bejesus out of regular || for defaults. Too bad it doesn't, one day maybe It makes for quite elegant oneliners. Also defaulting to development is a smart idea, you can fatfinger a serious trip to DB backups there. Embarrassing. I never realized that RAILS_ENV was literally an environment variable. I thought it was some kind of magical switch for rake... Does anybody know how they manage to set the variable at the end of the command? Testing in bash only works if I do something like: code:
code:
|
# ? May 4, 2013 10:17 |
|
I'm having some headache with Devise and routes. I'm wanting to go to a user's cellar instead of the root_path when a user signs in. A user's cellar is located at /users/:id/cellar with the following routes: cellar_user GET /users/:id/cellar(.:format) users#cellar I added the following to my action_controller: code:
NoMethodError at /users/sign_in undefined method `cellar_user' for #<Devise::SessionsController:0x434a00> What am I missing here?
|
# ? May 7, 2013 19:05 |
|
raej posted:But I'm getting a NoMethodError: cellar_user_path (or cellar_user_url)
|
# ? May 7, 2013 19:49 |
|
manero posted:cellar_user_path (or cellar_user_url) That's getting a different error, but I feel like it's moving: ActionController::RoutingError at /users/sign_in No route matches {:action=>"cellar", :controller=>"users"} My routes.rb for users looks like this: code:
|
# ? May 7, 2013 20:49 |
|
For anyone running nginx or recent enough Phusion Passenger, upgrade ASAP. http://mailman.nginx.org/pipermail/nginx-announce/2013/000112.html https://groups.google.com/forum/?fromgroups=#!topic/phusion-passenger/bbXdQReZX00
|
# ? May 7, 2013 21:08 |
|
raej posted:That's getting a different error, but I feel like it's moving: Ah, I'd say do "rake routes" from your terminal and see how it maps out. I'm fuzzy with how it would name it with all the nesting. It might be something like "devise_user_cellar_path(current_user)" or "user_cellar_path(current_user)"
|
# ? May 7, 2013 21:21 |
manero posted:Ah, I'd say do "rake routes" from your terminal and see how it maps out. I'm fuzzy with how it would name it with all the nesting. It might be something like "devise_user_cellar_path(current_user)" or "user_cellar_path(current_user)" Also, if you have a lot of routes (for a very large application), it can be handy to pipe rake routes into grep and give it a search term. Like `rake routes | grep mything`. It was a huge help getting acclimated to my first big rails project.
|
|
# ? May 7, 2013 21:26 |
|
DreadCthulhu posted:First of all, ||= is brilliant, glad you pointed that out. I don't write much Ruby these days, and so I had to immediately check if JS happened to have that operator, since I abuse the bejesus out of regular || for defaults. Too bad it doesn't, one day maybe It makes for quite elegant oneliners. Check out CoffeeScript, which is enabled by default in Rails installations.
|
# ? May 7, 2013 22:01 |
|
manero posted:Ah, I'd say do "rake routes" from your terminal and see how it maps out. I'm fuzzy with how it would name it with all the nesting. It might be something like "devise_user_cellar_path(current_user)" or "user_cellar_path(current_user)" That's the thing. I did rake routes and see this: code:
NameError at /users/1 undefined local variable or method `cellar_user' for #<#<Class:0x5194cf8>:0x2c62ba8>
|
# ? May 7, 2013 23:42 |
|
raej posted:That's the thing. I did rake routes and see this: cellar_user_path(current_user) You need to choose either _path (which will just be like /users/123/cellar), or _url, which will give you the host plus the path.
|
# ? May 7, 2013 23:50 |
|
Regarding Devise, does anybody know if and how Devise persists sessions across reboots of the web app? Does it happen to store them somewhere in a db, and if so, does it cache them in memory in any way to avoid having to hit the DB on every request that couldn't be authenticated in memory? Couldn't clearly see what's going on there from the source, the thing is pretty beefy.
|
# ? May 8, 2013 00:57 |
|
manero posted:cellar_user_path(current_user) aha! It was _path that was needed. Thank you so much!
|
# ? May 8, 2013 01:40 |
|
I want to override the I18n::translate method for my whole app. Why? Because the default scaffolded views use the f.label method and that calls translate and I want translate to conform to our special locale system. Normally it pulls the labels from the en.yml file under helpers.label.model.field I need to also have it check another location.
|
# ? May 8, 2013 15:05 |
|
DreadCthulhu posted:Regarding Devise, does anybody know if and how Devise persists sessions across reboots of the web app? Does it happen to store them somewhere in a db, and if so, does it cache them in memory in any way to avoid having to hit the DB on every request that couldn't be authenticated in memory? Couldn't clearly see what's going on there from the source, the thing is pretty beefy. Depends on your session storage strategy, i.e. it could be either cookies (default), your sql database via activerecordstore or something like memcached.
|
# ? May 8, 2013 16:10 |
|
|
# ? May 15, 2024 19:00 |
|
Smol posted:Depends on your session storage strategy, i.e. it could be either cookies (default), your sql database via activerecordstore or something like memcached. 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?
|
# ? May 8, 2013 18:00 |