Chris Oliver

Joined

290,710 Experience
86 Lessons Completed
298 Questions Solved

Activity

Posted in How do I publish an RSS feed from my rails app?

Hey Stan! Like an XML feed of them? If so, it's relatively easy. You've just got to create an index.xml.builder view which will let you render XML.

You can access all the instance variables you need from the controller just like a normal view so you can loop through the posts and print them out in the XML. I believe you'll want the tags and structure mentioned in the following example to conform to what the feeds need for tags.

Basecamp uses this for their feed, for example:

xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
  xml.channel do
    xml.title(@feed_title)
    xml.link(@url)
    xml.description "Basecamp: Recent items"
    xml.language "en-us"
    xml.ttl "40"

    @recent_items.each do |item|
      xml.item do
        xml.title(item_title(item))
        xml.description(item_description(item)) if item_description(item)
        xml.pubDate(item_pubDate(item))
        xml.guid(@person.firm.account.url + @recent_items.url(item))
        xml.link(@person.firm.account.url + @recent_items.url(item))

        xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
      end
    end
  end
end

Posted in How to send Webhook?

Btw, definitely doing a screencast on this soon.

Posted in How to send Webhook?

The cool part about webhooks is that they're just HTTP requests. All you have to do is collect a URL from your user, and then make a POST request to it with some data. I would recommend using rest-client for making the POST and sending it from a background job so that you can retry it if it fails. That's really all their is to it.

You ideally want to schedule your retries for extended periods of time, so if the first one fails, retry in a minute, retry in 5 minutes, retry in 10 minutes, 30 minutes, or something like that where there is some backoff. And then after X number of tries you can give up.

Posted in Rails csv import with associations

Posted in Rails csv import with associations

I'd toss a byebug in there and inspect each line to see what's causing it. Make sure a_categories is just an array of one item, then make sure categories is just an array of one Style, and so on.

Also keep in mind that you changed to operate on the Join table where I was showing querying the Category table instead. The reason for that was the query can verify those Category records exist.

Your code works differently than my example and needs to change a little if you do want to skip the verification that the Categories exist like mine did.

It's okay to do that, but instead of doing a where query, you would want to just do something like this:

a_categories.each do |category_id|
  post.styles.where(category_id: category_id).first_or_create
end

Note that this does not verify if a category ID is correct. If you put in say, category ID of 99999 and there was no Category record with that ID, you'll be inserting invalid data into the db. This might be fine, or it may be something you don't want, so I'd be careful with that.

Posted in Rails csv import with associations

Another thing to double check with this is to make sure that it's idemopotent, meaning if you import the same csv twice, it doesn't create duplicate categories per post. I'm not sure if assigning the categories like that will or not, but it's worth checking.

Posted in Rails csv import with associations

Oh derp. I wasn't paying attention whatsoever. :) My bad!

So if you've got the array of 1, 2, 3 and these match the ID column for your Category records, you can say

category_ids = ids # if this is a comma separated string of ids, you'll need to split(",") it into an array instead
categories = Category.where(id: category_ids)
post.categories = categories

This will automatically create the join records in the Styles table between the two and do it all in one batch because you'd be querying for the entire array of categories and then you can assign them using the association.

Of course, this assumes the categories are already in the database as a caveat.

Yessir! I'm still working on it. Had a couple things come up and had to postpone the episode a little bit. Working on the screencast this afternoon. :)

Posted in Rails csv import with associations

Sup Ryan! 👍 for formatting the markdown perfectly.

First off you'll need a join table to connect the two models. PostCategory can belongs_to :post and belongs_to :category and give you the ability for your post to has_many :categories, through: :post_categories. That will give you the ability to associate multiple categories to a post and reuse the categories on multiple posts.

The next question, of course, is how does your CSV format the categories in it? You'll basically just want to split those up, and then do a Category.find() on them to grab the categories and you can say something like post.categories << Category.find_by(name: category_name)

Posted in Hi, my name is...

What's up Ryan! 😎 Just wanted to say hi! Awesome to have you, and your projects look awesome!

@Nick stop it you. 😊

We gotta come together and make sure to crack the whip on Andrew so he doesn't die during his first half-marathon.

Posted in Deploying Sidekiq To Heroku Discussion

You'll want to check your Heroku logs to find out what crashed when you get the "Something Went Wrong" 500 error. It will tell you what code crashed.

If you deploy new migrations, you'll need to also run heroku run rails db:migrate to make sure production gets the new database changes as well.

Posted in Deploying Sidekiq To Heroku Discussion

I don't think you mentioned what the error is. What are you seeing?

Nothing really. I tried building a sample app to cache in Rails 5 with what we talked about and everything works as expected there. :\

1. It will auto index the new question, but only the new one that is added. It won't touch the existing ones. Searchkick implements callbacks on the models to automatically index them when they change.

3. You'd have to configure this. For example, you have to figure out what "popular" means. Is it views? Comments? etc. Once you figure out what you want, you can then pass in that data into your search_data so Searchkick indexes it and then you can query against it.

Posted in why .limit not working with find_by

For clarity, you might want to swap your variable names, because @categories is actual a single Category, and @category is an array of Categories.

Posted in Use .ENV instead of Secrets.yml for keys

You can use <%= ENV["AWS_SECRET_KEY"] %> in your secrets.yml to grab environment variables. For example:

production:
  aws_secret_key: <%= ENV["AWS_SECRET_KEY"] %>

The reason why you should continue using secrets.yml is because this provides a consistent interface if you stick with Heroku or migrate to your own server and choose to hardcode the values in a file on the server. Your Rails app doesn't have to care either way and you are free to use ENV variables or hardcoded values interchangeably.

Posted in How do I configure paperclip with capistrano?

Awesome! Yeah, the 12factor gem is useful for Heroku, but if you run your own server you don't want it as it won't write to that file.

Glad you got it working!

Posted in Message Templates Discussion

Yep, you can do that and use scopes in order to filter those as necessary.

Another alternative is a single Template table that stores templates for any object, where you store all the values in a serialized text, hstore or json column. The downside to this is that sometimes you can run into issues with data types not being consistent in the serialized ones.

Posted in Facebook Profile Image & Data using Devise in Rails

I'm not quite sure on #1, but as for #2, you've got paperclip which wants an actual file. Facebook's API is only going to return you the url of the file, so you've got to tell Paperclip that you want it to download the file from the URL.

    user.image = URI.parse(auth.info.image)

You'll want to modify your User method like this in order to actually assign a URI object to the image which Paperclip will know to download.

http://stackoverflow.com/questions/4049709/save-image-from-url-by-paperclip

I believe you're going to need it on the collection too because that's what the filter affects.