Chris Oliver

Joined

292,890 Experience
93 Lessons Completed
295 Questions Solved

Activity

Posted in Multiple Rubies using Rbenv

Yeah since bundler is a gem, you have to install it separately for each version of Ruby. Maybe they'll have it ship with Ruby at some point.

That definitely seems like the simplest approach that I've found over the years. I still haven't played with chruby, but it seems all the ruby version managers treat .ruby-version pretty consistently now so it shouldn't really matter what you use which is great!

Posted in Multiple Rubies using Rbenv

I simply set the Ruby version manually per project.

cd my-rails-app
echo "2.2.1" > .ruby-version
git add .ruby-version

When you cd into the directory, it should autodetect and switch for you.

You probably would want to do the same thing in your 1.9.3 apps to set their versions. If you do that, then your global ruby version doesn't matter since each app specifies what they need.

With that approach, you won't have to worry about wiping out any of your existing rubies or gems. rbenv will just seamlessly switch between whatever is already installed.

Posted in File Uploads with Refile Discussion

Any uploader that submits the file to S3 will work with Zencoder. They expect that your original video file is already available on S3 so they don't have to worry about which uploader you use. You're free to go with Refile, Carrierwave, Paperclip, etc.

Posted in When to use scoped models?

I'd say any time you're making supporting classes. For example User::Import should be a separate class from User to provide the functionality we want, but at the same time it's really closely tied together with User. That's usually a good sign it deserves being in a module like that.

If you use the generate model functionality, that would actually create a full ActiveRecord object and database table. In this case, I don't need to store the uploaded CSV files, so using an ActiveRecord model isn't necessary. We use ActiveModel::Model to give a regular Ruby class much of the same functionality as an ActiveRecord::Base object would have. That's what makes it easy to create a form_for @user_import.

Make sense?

Posted in Any good regex rules for validation?

Validating URLs is tough for the same reasons that validation on emails is tough. You can't verify the url or email is correct or live, only that it fits a format with regex.

It looks like Diego's regex here is the best option for correctness. https://mathiasbynens.be/demo/url-regex

Another great site for testing regex is http://rubular.com

It looks like on the Ransack docs, they do this:

class Artist
has_many :memberships
has_many :musicians, through: :memberships
end

artists = Artist.ransack(musicians_email_cont: 'bar')

Great post Andrew! That's a great approach and breakdown on the gotchas. You could either use a randomly generated ID of some sort, or you could add an incremental ID scoped to the company in as well. That can be nice so that you have more memorable ID numbers if you need them. This is how Github pull requests always start with #1. The real database ID is a separate number.

I'll be recording a couple episodes soon on how to set up multitenancy with subdomains. It's definitely not a simple thing, but really isn't too bad once you break it all down.

Posted in Setup MacOS 10.10 Yosemite Discussion

Oops, fixing!

Posted in Forum Series Part 2: Routes Discussion

Yeah the rollbacks only really happen when validations have failed to pass. That's something you'll pick up along the way and it helps point out what's wrong.

Best tip for debugging something like this is to make sure you've got the "byebug" gem installed and stick "byebug" into the controller before the save and then run the save manually from inside the byebug prompt. You'll get immediate output there letting you know if it worked or not and that can help save hours of debugging. :)

Posted in Forum Series Part 2: Routes Discussion

Looks like it's working correctly to me but validations caused it not to save. Double check all of those are passing.

Posted in Forum Series Part 2: Routes Discussion

You're really close. To create a form for a new comment on a recipe, you would do "form_for([@recipe, @comment])"

To create a new like on a recipe comment, you would do "<%= form_for([@recipe, @comment, @like]) do |f| %>"

I usually go for simplicity and don't use decorators for something like this. It's just a single method and I've already got a helper available so putting it there makes sense to me.

I find decorators are great for more complex things that manipulate record data into something more visible. A contrived example is a UserDecorator that has a "name" attribute that combines the "first_name" and "last_name" fields and does similar things for addresses. That rarely belongs in a model but it's a bit more logic than you'd want in a bunch of separate helper methods.

Posted in Setup Ubuntu 15.04 Vivid Vervet Discussion

Ah! Thanks. You find my copy-paste job from last version. ;)

Posted in Best way to render a page as a PDF

Unfortunately PDFs are a pretty tricky thing. I've been using Prawn, but it's annoying to use.

I haven't used it, but I think wicked_pdf is probably the way to go.

Posted in Exporting Records To CSV Discussion

I believe that ActiveRecord::Relation remembers which model class it originated from, so when you call "to_csv" on a Relation it knows to delegate it to the User class. This is how scopes work too. It functions in pretty much the exact same way.

Posted in Forms With Multiple Submit Buttons Discussion

Yes, published_at is a column and published? Is just a method delegating to published_at? so it's more readable.

Posted in Displaying an address from a selected location

It's basically gonna be like this I think with a regular select tag.

<%= f.select :location_id, @locations.map{ |c| [c.title, c.id, data: {icon: c.icon, description: c.description}] %>

Posted in Displaying an address from a selected location

This is pretty simple actually. Just did it on One Month's app. You'll add data attributes to the options in the select, and then you can grab those on change to display on the side. This is using simple_form, but you can convert this to a regular select_tag.

<%= f.input :location_id, as: :select, collection: @locations.map{ |c| [c.title, c.id, data: {icon: c.icon, description: c.description}] } %>

Elsewhere you would have an empty div for the location details:

<div class="location-details"></div>
jQuery ->
  $("#location_id").on "change", (e) ->
    selected = $("#location_id option:selected")
    $(".location-details").html("""
    <img src=#{selected.data("icon")} />
    <h3>#{selected.text()}</h3>
    <p>#{selected.data("description")}</p>
    """)

Of course, if you have an item pre-selected, you can have the JS fire the function on page load as well to use the selected data.

Yep, I believe so. Python makes it always explicit, but it can be a little confusing in Ruby since it isn't required to access but it is require to set attributes.

Posted in Upload Progress with Refile Javascript Discussion

I can but it might take a while. In the meantime, you can check out their backend section. It pretty much walks you through it all. Just need to know your Key and bucket name from AWS. Just create your S3 bucket first and fill in the rest.

https://github.com/refile/r...