Chris Oliver

Joined

290,590 Experience
86 Lessons Completed
296 Questions Solved

Activity

has_one and has_many are different and you'll interact with them differently.

has_one :content makes methods like @post.build_content and @post.create_content

has_many :contents makes methods like @post.contents.build and @post.contents.create

Much more information here: http://api.rubyonrails.org/...

Posted in Setup MacOS 10.10 Yosemite Discussion

zlib is missing; necessary for building libxml2

Make sure you installed all the dependencies (specifically zlib). Also, you almost never want to use "sudo" with this configuration.

Posted in Deploy Ubuntu 14.04 Trusty Tahr Discussion

You run capistrano every time you want to make your changes live on production. The shared directory ( and a symlink pointing to there ) is required so that you don't wipe out that file every deploy.

Posted in Deploy Ubuntu 14.04 Trusty Tahr Discussion

You'll want to create a secrets.yml just like you do with database.yml in the shared/config folder

Posted in Deploy Ubuntu 14.04 Trusty Tahr Discussion

Make sure to restart nginx after changing the configs. You can double check /var/log/nginx/error.log for Nginx errors if you get any issues.

Posted in Using Vagrant for Rails Development Discussion

Thanks for sharing!

Posted in Introduction to Importing from CSV Discussion

Ah thanks for the heads up. That was my mistake.

Posted in File Uploads with Refile Discussion

The best way to do it right now is to to upload each file to an associated has_many model.

Posted in Introduction to Importing from CSV Discussion

Ha! Thanks for catching that. I must have been tired.

Posted in Introduction to Importing from CSV Discussion

Great question. Either way works just the same in this case.

The only difference is that you have to worry about where the .save happens if you use that. With create, it doesn't really matter what order the next code is in. If you did save, you'd have to make sure it came first. Also, where the "save" happens is less clear if you have it on the right side of a line that has an "if" statement tacked on it.

In the end it doesn't make much difference at all because this code is very rough anyways. Ideally this gets refactored so that responsibilities are separated in a much cleaner fashion. More on that in a future episode though.

Posted in File Uploads with Refile Discussion

That's a great question. You should ask in an Issue on the github repo for Refile.

Posted in Setup MacOS 10.9 Mavericks Discussion

When you type "desktop" it thinks you want to use that as the filename. You want to type a full folder and filename. It suggests (and I do too) saving it to /Users/prince/.ssh folder with the name of id_rsa. That's the default and you can go with that because SSH will check there automatically to find keys. If you don't save it there, then you will need to specify where your key is every time you use SSH.

Posted in Dynamic Active Record Associations?

Yeah, you could just create one to remove the table. It'll keep things cleaner.

Posted in Javascript frameworks videos?

That's a good question. I've been trying to decide on frontend frameworks recently and have had a hard time. Ember and Angular are both awesome in their own ways so I'm really torn. I've really taken a liking to React recently and think it's possibly the one I'll do a deep dive with.

That said, I don't really know any of them well enough to put out great content yet. I'm open to suggestions and links to videos that might sway me one way or another!

Posted in How to create a dynamic form for Contest/Giveaway app

Now that I'm thinking about it, this would make for an awesome screencast.

Posted in How to create a dynamic form for Contest/Giveaway app

Don't worry, it's definitely a complex thing to wrangle. Not only do you have to deal with nested forms, but you also have to dynamically generate forms.

You can set a question_type attribute on Question to the string of boolean so that int the future you can use that attribute to determine what format of answer you need to record. As for recording answers, you may want to do something like a generic text field and then parse the result into a boolean so that you can easily add things like short answer questions later without having to update the Answer format.

The 4 models should be all you need and I'd recommend using something like Cocoon for making the Contest which would let you dynamically add more Questions to it. You won't need it for Entry because you'll know the questions and just need to generate one Answer per Question in the Contest to save with the Entry.

Your models should probably look something like this:

Contest

  • has_many :questions
  • has_many :entries

Question

  • belongs_to :contest
  • has_many :answers

Entry

  • belongs_to :contest
  • has_many :answers

Answer

  • belongs_to :entry
  • belongs_to :question

Posted in Importing datasets. Recommendations?

Comma separated CSV is usually pretty good standard to go with. Sometimes Excel saves with weird encodings though so it makes it less simple.

I think roo is pretty good for parsing files like these. I'd write a rake task to test it against a local file. The rake task can then just call a method a model so that it's in the correct place for use in your Rails app. Plus if you create a rake task, you can set it up to run nightly with a cron job if you ever had the need for that.

Posted in How to create a dynamic form for Contest/Giveaway app

Oh boy, the world of dynamic forms. Are you planning on having different types of Questions (like short answer, multiple choice)?

For the simplest thing, if I assume that each Question has the same type of answer (say they are all short answer), you can create an Entry with nested Answer records. Each Answer belongs_to a Question and also belongs_to Entry. You can then generate the form and dynamically add fields_for :answers for each of the questions and save the answers as part of the Entry (and validate them).

It gets more complicated if you want different types of questions though. Might check this out: http://vysakh0.github.io/complex-dynamic-forms-in-rails/

Posted in Dynamic Active Record Associations?

I would definitely consider it a code smell. The good thing is that if you are using it, you already have a join table and you can just create a new model file to represent it and swap your associations out.

It's usually pretty easy to swap out the associations because you'll basically use the same options only this time it has a defined Rails model for the join table instead of it being generated inside Rails at runtime.

Posted in Dynamic Active Record Associations?

I'd follow Bob's advice on this one. I was going to suggest the same thing. The main problem with has_and_belongs_to_many is that it doesn't let you access the join record easily so you need to swap it with an actual model named like EventPrice and do the has_many and belongs_to with that model.