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
Nybble
Jun 28, 2008

praise chuck, raise heck
Rails for Zombies was interesting, but it has nothing on going through Michael Hartl's online book and building a full app yourself. It really hits on testing ALOT, which might be a hurdle you don't want to mess with, but it was definitely a great jumping off point for me learning Ruby AND Rails from scratch.

http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Adbot
ADBOT LOVES YOU

shdwdmg
May 16, 2008
A bit late to the party, but realistically speaking I am looking for a career change. I have some programming experience from HS, but opted for a stupid Liberal Arts Degree that has offered dismal opportunities.

What would I have to do to be taken serious when I start to look for a job? or Should I not even bother at this point?

EAT THE EGGS RICOLA
May 29, 2008

shdwdmg posted:

What would I have to do to be taken serious when I start to look for a job? or Should I not even bother at this point?

Having an awesome portfolio and being good at networking will be enough.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

shdwdmg posted:

A bit late to the party, but realistically speaking I am looking for a career change. I have some programming experience from HS, but opted for a stupid Liberal Arts Degree that has offered dismal opportunities.

What would I have to do to be taken serious when I start to look for a job? or Should I not even bother at this point?

Learn enough Rails to build some apps, be able to be confident and friendly in an interview, and be willing to relocate. I know a few shops in Miami that are desperate to hire, and my understanding is that places in NYC, SF, Chicago, etc. are also having trouble finding enough developers.

shdwdmg
May 16, 2008

Cocoa Crispies posted:

Learn enough Rails to build some apps, be able to be confident and friendly in an interview, and be willing to relocate. I know a few shops in Miami that are desperate to hire, and my understanding is that places in NYC, SF, Chicago, etc. are also having trouble finding enough developers.

I am in the Chicago suburbs, so this seems promising.

soullessshoe
Nov 6, 2011

Cocoa Crispies posted:

I did this with non-programmers.

  • Don't waste time with the Ruby Koans
  • Rails for Zombies is kinda poo poo but the exercises are okay to drill with maybe
  • Use Sublime unless you also feel like teaching IDEs with complicated and confusing interfaces or 1970s editors with non-modern interfaces
  • If students bring their own computers, get ready for "fun" when you find out non-Rails-people don't all have MacBooks with Mountain Lion

Good luck!

cool thanks. yeah, planning on recommending sublime text and setting up a vagrant box to have people boot up.


shdwdmg posted:

A bit late to the party, but realistically speaking I am looking for a career change. I have some programming experience from HS, but opted for a stupid Liberal Arts Degree that has offered dismal opportunities.

What would I have to do to be taken serious when I start to look for a job? or Should I not even bother at this point?

Attend meetups and share any for fun projects you do =)

KoRMaK
Jul 31, 2012



shdwdmg posted:

A bit late to the party, but realistically speaking I am looking for a career change. I have some programming experience from HS, but opted for a stupid Liberal Arts Degree that has offered dismal opportunities.

What would I have to do to be taken serious when I start to look for a job? or Should I not even bother at this point?
Like the visual effects industry, programming jobs can be landed by solid portfolios. Namely ones that interact with a DB.

manero
Jan 30, 2006

shdwdmg posted:

A bit late to the party, but realistically speaking I am looking for a career change. I have some programming experience from HS, but opted for a stupid Liberal Arts Degree that has offered dismal opportunities.

What would I have to do to be taken serious when I start to look for a job? or Should I not even bother at this point?

Look for a local Ruby or Rails user group -- I organize the MN Ruby user group, and it's an awesome way to learn and network with people.

kayakyakr
Feb 16, 2004

Kayak is true

manero posted:

Look for a local Ruby or Rails user group -- I organize the MN Ruby user group, and it's an awesome way to learn and network with people.

This is a good idea. I know Austin has one that is filled with companies looking for Rails people. They do monthly meetings with two presentations where the first is geared toward rails beginners.

mmachine
Jan 5, 2006
Can anyone offer suggestions on how I should be using CanCan to control permissions for records that are related to parent records? For example, I have a Book model which I can enforce :manage permissions on based on an author.id I have in my sessions (via Devise). I know there's a few ways to go about it, but my current ability.rb for CanCan looks like:

code:
can :manage, Book do |book|
	book.author_id == author.id
end
Which works fine. What I'm trying to figure out is how to descend that permission to any sub records. In this case, Book models can have children records of a Page model. So my book.rb goes like:

code:

has_many :pages

So question is, how do I get the Book CanCan permissions defined already in my ability.rb to descend to child record(s) of the Book model? CanCan documentation reads like this should happen based on my native model definitions for the app, but that doesn't seem to be the case. Do I also need to define relationships within CanCan's Ability model or what? If so, is there a decent example someone can provide?

EDIT: I also tried using this method in my applicable app controllers:

code:
load_and_authorize_resource :book
load_and_authorize_resource :page, :through => :book
...which didn't work for me either.

kayakyakr
Feb 16, 2004

Kayak is true

mmachine posted:

So question is, how do I get the Book CanCan permissions defined already in my ability.rb to descend to child record(s) of the Book model? CanCan documentation reads like this should happen based on my native model definitions for the app, but that doesn't seem to be the case. Do I also need to define relationships within CanCan's Ability model or what? If so, is there a decent example someone can provide?

How are you assigning to the pages class? If you aren't assigning via the book class, then it probably won't pick up the CanCan ability. I'm not sure if just doing book.pages.create will be enough to assign through books.

mmachine
Jan 5, 2006

kayakyakr posted:

How are you assigning to the pages class? If you aren't assigning via the book class, then it probably won't pick up the CanCan ability. I'm not sure if just doing book.pages.create will be enough to assign through books.

I'm assigning page classes for my application via the core model definitions for my app. That relationship has been in place for a while now, and works no problem. The CanCan implementation is pretty recent, which is why I'm kind of confused as to where I'm not re-affirming these relationships for CanCan.

When you say assign via the book class, do you mean via a CanCan-specific method?

kayakyakr
Feb 16, 2004

Kayak is true

mmachine posted:

I'm assigning page classes for my application via the core model definitions for my app. That relationship has been in place for a while now, and works no problem. The CanCan implementation is pretty recent, which is why I'm kind of confused as to where I'm not re-affirming these relationships for CanCan.

When you say assign via the book class, do you mean via a CanCan-specific method?

Sorry, I was wrong on how CanCan is implemented. New set of questions about your implementation:
Are you using shallow routes (/pages/:id) or deep routes (/books/:book_id/pages/:id)?
Outside of the load_and_authorize_resource block, how are you using CanCan?

One note in my searches that I ran across: to use load_and_authorize, CanCan needs some sort of searching done on its ability. Your ability file could theoretically be designed as
code:
can :manage, Book, :author => {:id => author.id}
That should help the second part work, maybe?

mmachine
Jan 5, 2006

kayakyakr posted:

Sorry, I was wrong on how CanCan is implemented. New set of questions about your implementation:
Are you using shallow routes (/pages/:id) or deep routes (/books/:book_id/pages/:id)?
Outside of the load_and_authorize_resource block, how are you using CanCan?

One note in my searches that I ran across: to use load_and_authorize, CanCan needs some sort of searching done on its ability. Your ability file could theoretically be designed as
code:
can :manage, Book, :author => {:id => author.id}
That should help the second part work, maybe?

Oh no worries. I am using deep / nested resource routes. Goes a little like you described:

code:
resources :books do 
	resources :pages
end

The example you gave with the can :manage, Books is the right track -- that's the part I've got figured out already, and that enforces permissions on Books ONLY. I'm declaring it a bit differently than you described, but same outcome. In my ability.rb in my app that looks like this:

code:

can :manage, Book, do |book|
	book.author_id == author.id
end

Which allows the following. Say I have book ID #44, I can get to this properly:

code:
/books/44/edit/
...but the minute I try to go like:

code:
/books/44/pages/22/edit
I get the applicable CanCan access denied error. It's like there's no acknowledgement at all of the parent Book's owner -- it's trying to validate the Page record permissions on its own.

EDIT: So I think I figured this out, but it feels kind of awkward because I had to hop back from the child record to reference the parent. Like so:

code:
can :manage, Book do |book|
	book.author_id == author.id
  	
	can :manage, Page do |page|
		page.book.author_id == author.id
	end
end
Where I go page.book.author_id feels kind of an added step in my brain for some reason given that my model relationships for the app are already in place. Probably just over thinking this though? A permissions system probably should require that step for the sake of security -- I might have just gotten too used to RoR handling a lot more of that for me. DISGUSTED by my own laziness right now.

In any case, with my own personal QA it looks like the child records now follow ownership of their parents. Can anyone weigh in on whether or not I'm on the right track?

mmachine fucked around with this message at 17:41 on Jun 25, 2013

KoRMaK
Jul 31, 2012



That's how you do it. I've recently upgraded our file with a similar parent chaining with added consideration if the parent isn't attached yet.

it is
Aug 19, 2011

by Smythe
I'm doing QA for a small company and I'm trying to modify their test infrastructure so it doesn't need to touch an actual database and do real database operations. I spent all day trying to get NullDB to work but I'm terrible with, like, config files and stuff. Is there any nice way I can tell Rails to not actually save the variables to the hard disk? And instead have a bunch of variables floating around that are basically like database objects but they get garbage-collected when they fall out of scope? Because that would be, like, the ideal scenario. Not going around and rewriting every single test and instead putting some code in there saying "when I say Lesson.create I ACTUALLY mean Lesson.mostly-create-but-not-save-because-like-seriously-it-is-unnecessary-to-go-to-the-database"

I'm mostly trying to avoid spending an entire day doing that again D:

Anveo
Mar 23, 2002

it is posted:

I'm doing QA for a small company and I'm trying to modify their test infrastructure so it doesn't need to touch an actual database and do real database operations. I spent all day trying to get NullDB to work but I'm terrible with, like, config files and stuff. Is there any nice way I can tell Rails to not actually save the variables to the hard disk? And instead have a bunch of variables floating around that are basically like database objects but they get garbage-collected when they fall out of scope? Because that would be, like, the ideal scenario. Not going around and rewriting every single test and instead putting some code in there saying "when I say Lesson.create I ACTUALLY mean Lesson.mostly-create-but-not-save-because-like-seriously-it-is-unnecessary-to-go-to-the-database"

I don't have a solution for not writing to the DB at all, but if your queries are standard AR stuff then SQLite supports running a database in memory. Just use "memory" as the database location in your config.

KoRMaK
Jul 31, 2012



I have two objects/tables, Persons and feed_entries. A person can have multiple feed_entries. I want to set the default_scope for Persons so that it orders DESC by the "last_updated" method which is defined below on person.rb
Ruby code:
def last_updated
    last_feed = feed_entries.order("created_at DESC").first.created_at rescue nil
    if (last_feed == nil) || (updated_at > last_feed)
      last_feed = updated_at 
    end
    last_feed
  end
Here is the best default_scope I could come up with, but it's wrong because it sorts by both columns. I want it to sort by whichever one is more recent, as demonstrated by last_updated.
Ruby code:
default_scope joins("LEFT JOIN feed_entries ON feed_entries.person_id = persons.id").order('feed_entries.created_at DESC, persons.updated_at DESC').group('persons.id')
I've tried using the last_updated method in the scope with a lambda but it says that "Class does not have a last_updated method" so I guess using it in the scope is out? So here is some psudeocode to try and convey as best as i can what I would like to be able to do
Ruby code:
default_scope order(last_updated DESC)

KoRMaK fucked around with this message at 22:48 on Jun 27, 2013

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

KoRMaK posted:

I have two objects/tables, Persons and feed_entries. A person can have multiple feed_entries. I want to set the default_scope for Persons so that it orders DESC by the "last_updated" method which is defined below on person.rb
Ruby code:
def last_updated
    last_feed = feed_entries.order("created_at DESC").first.created_at rescue nil
    if (last_feed == nil) || (updated_at > last_feed)
      last_feed = updated_at 
    end
    last_feed
  end
Here is the best default_scope I could come up with, but it's wrong because it sorts by both columns. I want it to sort by whichever one is more recent, as demonstrated by last_updated.
Ruby code:
default_scope joins("LEFT JOIN feed_entries ON feed_entries.person_id = persons.id").order('feed_entries.created_at DESC, persons.updated_at DESC').group('persons.id')
I've tried using the last_updated method in the scope with a lambda but it says that "Class does not have a last_updated method" so I guess using it in the scope is out?
Ruby code:
default_scope order(last_updated DESC)

last_updated is on the Ruby class, not the SQL table.

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!
This would probably work if you wanted to do that ordering by a scope:

code:
	joins("LEFT JOIN feed_entries ON feed_entries.person_id = persons.id")
        .order('CASE WHEN persons.updated_at > feed_entries.created_at THEN persons.updated_at ELSE feed_entries.created_at END')
        .group('persons.id')
However, this kind of seems like a bad idea. Default scopes are scary at the best of times, JOINING in them seems like a recipe for disaster. At the very least, you're introducing the potential for a lot of performance problems.

It sounds like what you're really trying to accomplish is to have the rules for when a person is updated include whether they have had a feed entry created. It might just be simpler to change the person's updated_at when the feed entry is created and avoid all this crazy joining in the first place:

code:
   # in feed_entry.rb
   after_create { person.touch }
.touch just updates the updated_at of whatever record you specify (you can specify another date column if you want to).

prom candy
Dec 16, 2005

Only I may dance
You can do that right in the relationship.

code:
  class FeedEntry < ActiveRecord::Base
    belongs_to :person, touch: true
  end

KoRMaK
Jul 31, 2012



Yes that makes so much sense. Way more rails than whatever I was envisioning.

KoRMaK
Jul 31, 2012



Has anyone had to deal with devise's password reset in a multi tenantcy environment? Devise is only looking for users by email, I need email and account_id in my user load query. I'm going through the devise library and feels like I'm getting close but there has got to be someone else that has had this issue.

I'm currently overriding the devise password controllers create method but by the time it gets to user.find first by auth conditions it has forgotten the account id I am passing in.

Molten Llama
Sep 20, 2006
How'd you set Devise up in your initializer? If you haven't already tweaked your config.authentication_keys and config.request_keys, start there and the rest should fall into place a lot more quickly and obviously.

KoRMaK
Jul 31, 2012



Molten Llama posted:

How'd you set Devise up in your initializer? If you haven't already tweaked your config.authentication_keys and config.request_keys, start there and the rest should fall into place a lot more quickly and obviously.
Only noticed the authentication_keys and not the request_keys. Thanks I'll check that out.

xtal
Jan 9, 2011

by Fluffdaddy
Given an exported GPG private key and its passphrase, how can I decrypt a string encrypted with the corresponding public key? Haven't had any luck with GPG-related gems, which all seem to rely on pinentry/gpg-agent instead of a provided passphrase.

tima
Mar 1, 2001

No longer a newbie

xtal posted:

Given an exported GPG private key and its passphrase, how can I decrypt a string encrypted with the corresponding public key? Haven't had any luck with GPG-related gems, which all seem to rely on pinentry/gpg-agent instead of a provided passphrase.

I don't know how deep you want to dig into it, but PGP/GPG uses https://tools.ietf.org/html/rfc4880 for algorithm, it should be fairly easy to implement (code wise) if you have time to dig into the math.

That said this gem looks like it doesn't have too many dependencies, although I haven't used it myself: http://openpgp.rubyforge.org/

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

xtal posted:

Given an exported GPG private key and its passphrase, how can I decrypt a string encrypted with the corresponding public key? Haven't had any luck with GPG-related gems, which all seem to rely on pinentry/gpg-agent instead of a provided passphrase.

Install GPG, spawn it with the passphrase being echo'd in. The GPG gems are all pretty much garbage.

tima posted:

I don't know how deep you want to dig into it, but PGP/GPG uses https://tools.ietf.org/html/rfc4880 for algorithm, it should be fairly easy to implement (code wise) if you have time to dig into the math.

DO NOT under any circumstances do this. You'll gently caress it up in an unintuitive way and waste a lot of time in the process.

xtal
Jan 9, 2011

by Fluffdaddy
Installing GPG is fine, but I'd like to do it without needing to --import the key or deal with pinentry. Suppose this is more of a GPG question than a Ruby question now, though.

xtal fucked around with this message at 02:35 on Jul 9, 2013

soullessshoe
Nov 6, 2011
I hate timezones... anyways..

Am i doing something wrong with this?


Ruby code:
irb> Time.zone = ActiveSupport::TimeZone[-240.minutes]
=> (GMT-04:00) Atlantic Time (Canada)
irb> Time.zone.now
=> Tue, 09 Jul 2013 12:19:39 ADT -03:00
irb> Time.now.in_time_zone
=> Tue, 09 Jul 2013 12:19:42 ADT -03:00
is this a solution?

Ruby code:
irb> Time.zone = ActiveSupport::TimeZone.all.detect {|z| z.now.utc_offset == -240.minutes.to_i }
=> (GMT-05:00) Eastern Time (US & Canada)
raise your hands if you hate timezones.

edit:

I'm guessing timezone lookup is done using standard offset, whereas the 240 minutes comes from javascript (new Date()).getTimezoneOffset() (which is affected by daylight savings)

soullessshoe fucked around with this message at 16:39 on Jul 9, 2013

kayakyakr
Feb 16, 2004

Kayak is true
Time Zones in any language are a huge pain in the rear end.

If you have a particular zone you want the time in, you can use

code:
  DateTime.now.in_time_zone("Eastern Time (US & Canada)")
This will take care of DST and any zone stuff you need. If that zone's gotta be dynamic, well... good luck?

Anveo
Mar 23, 2002
A good read is The Exhaustive Guide to Rails Time Zones

Pardot
Jul 25, 2001




Timezone chat: The default datatype rails uses for timestamps is `timestamp` which is terrible. You should always use `timestamptz`

Here is an example migration that will fix the awful

code:
class Timestamptz < ActiveRecord::Migration
  def up
   execute "alter table authorizations  alter column created_at              set data type timestamptz"
   execute "alter table authorizations  alter column updated_at              set data type timestamptz"
   .. etc

soullessshoe
Nov 6, 2011
Regarding the timezones: the offset is coming from client side, being saved to a cookie, then read server-side.

problem is that the current offset is passed from client side whereas ActiveSupport::TImeZone#[] does the lookup by standard timezone offset.

Hence the reason I am doing the crazy
Ruby code:
 zone.now.utc_offset
comparison

kayakyakr
Feb 16, 2004

Kayak is true

soullessshoe posted:

Regarding the timezones: the offset is coming from client side, being saved to a cookie, then read server-side.

problem is that the current offset is passed from client side whereas ActiveSupport::TImeZone#[] does the lookup by standard timezone offset.

Hence the reason I am doing the crazy
Ruby code:
 zone.now.utc_offset
comparison

Ok, so my understanding might be too simple, but how about outputting date to string, parsing it server side, and taking what you get in ruby? With some manipulation it would give you the right zone without worrying about DST.

JS:
code:
(new Date()).toString();
ruby
code:
date = DateTime.parse(in_date)
date.zone == '-05:00'

Newbsylberry
Dec 29, 2007

I Swim in drag shorts because I have a SMALL PENIS
I am a newbie and stuck trying to get nested resources and associations to work. I used scaffolding to generate my DB migrations, controller, routes, and views for a training app that I am working on to help myself get comfortable with rails. I nested my resources in routes.rb:

code:
Seemccarthy::Application.routes.draw do
  
  resources :athletes do
    resources :training_plans
  end
I made my associations in each model:

athlete.rb
code:
class Athlete < ActiveRecord::Base 
  has_many :training_plans

...
end
training_plan.rb
code:
class TrainingPlan < ActiveRecord::Base
  belongs_to :athlete
end
and pointed my _form.rb to the new path:
code:
<%= form_for(@training_plans, :url=>new_athlete_training_plan_path(@athlete)) do |f| %>
I can show the training plans for an athlete, I can get the form to create a new athlete training plan, but then when I click create I get the following error:

No route matches [POST] "/athletes/1/training_plans/new"

These are my rake routes:
(screen shot to save tables)


And here is my training_plans_controller.rb file:

code:
class TrainingPlansController < ApplicationController
  before_action :set_training_plan, only: [:show, :edit, :update, :destroy]

  # GET /training_plans
  # GET /training_plans.json
  def index
    @athlete = Athlete.find(params[:athlete_id])
    @training_plans = @athlete.training_plans.all
  end

  # GET /training_plans/1
  # GET /training_plans/1.json
  def show
  end

  # GET /training_plans/new
  def new
    @athlete = Athlete.find(params[:athlete_id])
    @training_plans = @athlete.training_plans.build
  end
 
  # GET /training_plans/1/edit
  def edit
  end

  # POST /training_plans
  # POST /training_plans.json
  def create
    @athlete = Athlete.find(params[:athlete_id])
    @training_plans = @athlete.training_plans.build(params[training_plan_params])
I know this is a lot, but I have scoured the web, and read through the routing article in rails guide, as well as the associations article, and I just count seem to figure out the problem. I even tried starting over from the beginning but keep running into the same issue. If someone could tell me what I'm doing wrong, and how to fix it in the future, it would be much appreciated. Thanks!

Newbsylberry fucked around with this message at 20:08 on Jul 10, 2013

kayakyakr
Feb 16, 2004

Kayak is true
New is a view-only route. The create is triggered by POSTing the index route, ie athlete_training_plans_path.

The route helper you should be using shows on the left side of the route output and is the last one listed above. Create (POST) is athlete_training_plans, Update (PUT) and destroy (DELETE) are both on athlete_training_plan. New and Edit just get the forms, you don't post back to them.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
Can anyone point me towards some good resources for Chef recipes?

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
We watched a peepcode screencast about chef at work, it was pretty decent.

Adbot
ADBOT LOVES YOU

waffle enthusiast
Nov 16, 2007



Lexicon posted:

Can anyone point me towards some good resources for Chef recipes?

I think the opscode site has some pretty good resources itself. Just curious - any reason you picked chef over puppet?

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