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
Pardot
Jul 25, 2001




I have no idea, really. I have one rails project I inherited from contractors and I just did this

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"

   execute "alter table clip_results    alter column started_at              set data type timestamptz"
   execute "alter table clip_results    alter column finished_at             set data type timestamptz"
for every column.

My suggestions should be taken with the filter that I may also be brain damaged from this long on a postgres infrastructure team. I recently wrote

Ruby code:
  
get "/list" do
    @overview = DB[<<-SQL].all
      select
        v::date as visiting_on,
        count(visiting_on) as total,
        count(nullif(lunch, false)) as lunch
      from generate_series(now(), now() + '2 weeks'::interval, '1 day') as v
      left outer join guests on v::date = visiting_on::date
      group by 1
      order by 1 asc;
    SQL

    @day = params[:day] || Date.today.to_s
    @day_guests = DB['select * from guests where ?::date = visiting_on::date', @day].all

    erb :list
  end
end
for example, and think it's fantastic.

Adbot
ADBOT LOVES YOU

Sil
Jan 4, 2007
Rspec is loving with me tonight. I think.

I have a setup with sucker_punch(a celluloid thingy) that sends emails asynchronously for me. It works perfectly in production and development.

When I go to test it with Rspec and Capybara suddenly database records start vanishing the moment my application switches into the sucker_punch class(aka the celluloid actor). I try to use sucker_punch's inline testing whatever options, does nothing. It doesn't wipe all records, I don't have Database Cleaner running. I am at a complete loss.

kayakyakr
Feb 16, 2004

Kayak is true

Sil posted:

Rspec is loving with me tonight. I think.

I have a setup with sucker_punch(a celluloid thingy) that sends emails asynchronously for me. It works perfectly in production and development.

When I go to test it with Rspec and Capybara suddenly database records start vanishing the moment my application switches into the sucker_punch class(aka the celluloid actor). I try to use sucker_punch's inline testing whatever options, does nothing. It doesn't wipe all records, I don't have Database Cleaner running. I am at a complete loss.

Are you trying to send emails from an after_save? If so, you're probably hitting a race condition and should switch to after_commit.

Sil
Jan 4, 2007

kayakyakr posted:

Are you trying to send emails from an after_save? If so, you're probably hitting a race condition and should switch to after_commit.

Holy moly, you're my god!

That being said I still think it's weird. I checked the database during the before_save and the record is there, then I check the database in the after_save and the record isn't there anymore. This likely reflects my misunderstanding of how ActiveRecord works, but still. Also it was never a problem in development/production. You know, when actually sending the email over smtp. Presumably the lag of authorizing with gmail allowed AR to catch up with itself? Or something?

Granted I don't quite understand how Celluloid/sucker_punch works either. Time to make some additions to the READTHIS bookmarks folder.

prom candy
Dec 16, 2005

Only I may dance
ActiveRecord callbacks can make testing really difficult. You might want to consider moving the entire behaviour of "save an object, send an email" into a little service class. That way you can test your object and your email sending separately, and then test the service class just to make sure it sends the right messages in the right situation. A lot of people will tell you that you should only use an AR callback when the callback concerns your persistence layer, ie to change or set some data for storage.

http://samuelmullen.com/2013/05/the-problem-with-rails-callbacks/

Sil
Jan 4, 2007

prom candy posted:

ActiveRecord callbacks can make testing really difficult. You might want to consider moving the entire behaviour of "save an object, send an email" into a little service class. That way you can test your object and your email sending separately, and then test the service class just to make sure it sends the right messages in the right situation. A lot of people will tell you that you should only use an AR callback when the callback concerns your persistence layer, ie to change or set some data for storage.

http://samuelmullen.com/2013/05/the-problem-with-rails-callbacks/

Nice link, I think I hurt my neck from too much nodding as I was reading it. That being said even if I did refactor the Article class(which is my #1 priority atm and the reason I'm finally writing my end-to-end tests) I'd still want to test the entire behavior in one go. Changing the callback to after_commit worked nicely, but as soon as I have all my site behavior on auto-test mode I'll look to refactor the damned god class away.

kayakyakr
Feb 16, 2004

Kayak is true

Sil posted:

Holy moly, you're my god!

That being said I still think it's weird. I checked the database during the before_save and the record is there, then I check the database in the after_save and the record isn't there anymore. This likely reflects my misunderstanding of how ActiveRecord works, but still. Also it was never a problem in development/production. You know, when actually sending the email over smtp. Presumably the lag of authorizing with gmail allowed AR to catch up with itself? Or something?

Granted I don't quite understand how Celluloid/sucker_punch works either. Time to make some additions to the READTHIS bookmarks folder.

If you're seeing the record in the database in a before_save, then you're doing something wrong as before save literally means before it's been saved. The after save should show the database record, though.

I'll explain to you why it wasn't working in test mode but was in production/development, though. Every save/update/delete/etc in rails is wrapped in a transaction (or psuedo-transaction). When you're inside the scope of that transaction, everything works fine, but outside that scope, nothing that you've done inside can be seen.

In production/development, sucker_punch (or in other words, celluloid) gets that async call, spins up a new thread, and then does what it's going to do. This creates a race condition, but it's taking long enough that the save wins the race most of the time. I'm guessing that if you load the system down, the save will start to lose the race and you'll get a bunch of failed emails.

In test mode, sucker_punch runs everything single-threaded, no async, but still manages to access active record outside of the scope of the transaction. So you spin up the email service before committing the transaction, the email service is looking from the outside-in and can't see what's gone on inside the transaction.


And I've never liked the idea of wrapper classes. It's a neat idea, but you start abstracting behaviors away from the classes that they're managing and now have two places where people can screw things up. Though using a wrapper class to manage saves is, in my opinion, a better pattern than observers which always seemed so loose to me.

Sil
Jan 4, 2007
Race condition is exactly what was going on. I had

code:
class SentMail < ActiveRecord::Base
  belongs_to :article
  belongs_to :subscriber

class Subscriber < ActiveRecord::Base
 has_many :sent_mails
 has_many :articles, :through => :sent_mails

class List < ActiveRecord::Base
  has_and_belongs_to_many :subscribers
  has_and_belongs_to_many :articles

class Article < ActiveRecord::Base
 has_many :sent_mails
 has_many :subscribers, :through => :sent_mails
 before_save :load_subscribers
 after_save :send_newsletter

private
  def load_subscribers
    self.lists.each do |list|
      list.subscribers.confirmed.each do |sub|
        self.subscribers<<sub unless self.subscribers.include? sub
      end
    end
  end
The funkiness is that I was loading Subscribers to the article in a before_save callback(from the List table which is in a habtm with Article and Subscribers. Which ... works? I'm not sure how to feel about that arrangement) which was also creating the SentMail records.

I thought that if I then sent emails in an after_save callback all the records would be nicely instantiated, but obviously that was not the case, I didn't think about the time window in between AR sending a save command to the DB and processing after_save callbacks and the DB actually executing the commit.

As it stands I moved the email sending callback to the SentMail class in an after_commit there, which seems pretty reasonable, since SentMail also keeps track of whether a given subscriber has already received a given article or not. Definitely the most interesting problem I've stumbled upon in my brief time with Rails.


Testing: better late than never! This would have sucked balls when it inevitably happened after product delivery.


e. What I'm wondering now is how the newsletter process is going to behave on Heroku. Before it spawned one process that sent ~50-100 emails. Now it spawns ~50-100 processes that send one email each? Probably still a bad idea.

Sil fucked around with this message at 15:13 on Sep 27, 2013

Chilled Milk
Jun 22, 2003

No one here is alone,
satellites in every home
What's a good way of presenting a way for users to add an event to their calendar? Specifically on mobile? Generating an .ics seems to work well enough for desktop and iPhones, but android doesn't seem to know what to do with it without a separate app.

Edit: Peculiar, my s3 on Cyanogenmod doesn't want to handle any sort of ics file, but the office Tab 2 we have for testing works great. Now I'm wondering if it's just the stock calendar app that doesn't support it. Or maybe my phone is just borked.

Chilled Milk fucked around with this message at 18:31 on Sep 27, 2013

Molten Llama
Sep 20, 2006
The most reliable solution I've found for Android is to use a Google Calendar button. Even that's not 100%.

This might possibly improve in the distant future since Ice Cream Sandwich finally introduced a calendar provider, but I'm not holding my breath. The calendar situation on Android is bad, bad, bad.

Sil
Jan 4, 2007

Sil posted:


e. What I'm wondering now is how the newsletter process is going to behave on Heroku. Before it spawned one process that sent ~50-100 emails. Now it spawns ~50-100 processes that send one email each? Probably still a bad idea.

Actually it was just 2 processes max but when it has 50 emails in the queue I get to visit Seg-Fault Heaven and the weather is lovely and incomprehensible. Presumably some combination of Celluloid, Nokogiri and Premailer are screwing the pooch.

e. Ah libxml, how I hate your guts. Good thing it's poo poo for so many smarter people.

Sil fucked around with this message at 22:17 on Sep 27, 2013

wolffenstein
Aug 2, 2002
 
Pork Pro
I'm trying to make this snippet for extending the Mechanize gem work with my Rails 4 project, but the only advice I got from #rubyonrails was put in in the /lib directory. Numerous searches show how to add /lib to autoload, but they don't seem to work with the snippet. Any ideas?

kayakyakr
Feb 16, 2004

Kayak is true

wolffenstein posted:

I'm trying to make this snippet for extending the Mechanize gem work with my Rails 4 project, but the only advice I got from #rubyonrails was put in in the /lib directory. Numerous searches show how to add /lib to autoload, but they don't seem to work with the snippet. Any ideas?

You shouldn't make a habit of it, but a lot of people throw monkey patches into initializers. I would suggest requiring the file manually, instead. i'm not a fan of adding your entire lib to autoload unless you really need to.

wolffenstein
Aug 2, 2002
 
Pork Pro
I'm still getting NoMethodError though.

edit: the snippet says "class WWW::Mechanize::CookieJar", but the correct class is "class Mechanize::CookieJar". After this, the methods were recognized, but they don't retrieve the current state of @jar. It's just an empty hash.

wolffenstein fucked around with this message at 18:33 on Sep 28, 2013

Coco13
Jun 6, 2004

My advice to you is to start drinking heavily.

Legacyspy posted:

I have a static "info" page under public/info.html.

Is there a simple way to like, have a link to that page on the bottom of every page in my application? Or do I have to add it manually to every view?

Other's have mentioned it, but here's where I learned about layouts, rendering bits, and all that jazz. I bought the book a while back, and it's a great resource, but if you already have a solid foundation you could probably get by with just trying to find this info on the website.

KoRMaK
Jul 31, 2012



I'm starting my first app from scratch. I have about 2 years in on another project that I inherited after it was setup with a custom config. There isn't really a problem, but I notice a difference when I scaffold. With my new app the models have all their variables mentioned via attr_accessible. The other app doesn't do this. The only time it does is for when something gets added to the account model. My guess is that the account model was made before the gem or config option that controls this. Can anyone suggest what gem/option controls this?

Oh My Science
Dec 29, 2008
I think you may be referring to Strong Parameters.

kayakyakr
Feb 16, 2004

Kayak is true

KoRMaK posted:

I'm starting my first app from scratch. I have about 2 years in on another project that I inherited after it was setup with a custom config. There isn't really a problem, but I notice a difference when I scaffold. With my new app the models have all their variables mentioned via attr_accessible. The other app doesn't do this. The only time it does is for when something gets added to the account model. My guess is that the account model was made before the gem or config option that controls this. Can anyone suggest what gem/option controls this?

What version of rails is your old project? And your new? There have been significant changes in rails over the years, first adding attr_accessible and then switching to the aforementioned strong parameters. If you're generating a new app, I would highly suggest choosing rails 4 now so you don't have to deal with upgrade heartburn later.

KoRMaK
Jul 31, 2012



Ruby 1.9.2, Rails 3.2.13. They both have that in common. Strong Parameters doesn't look familiar. The older project inherits a proprietary setup called Rails Saas kit so maybe its from that.

KoRMaK fucked around with this message at 01:10 on Oct 3, 2013

kayakyakr
Feb 16, 2004

Kayak is true

KoRMaK posted:

Ruby 1.9.2, Rails 3.2.13. They both have that in common. Strong Parameters doesn't look familiar. The older project inherits a proprietary setup called Rails Saas kit so maybe its from that.

Rails 3.2 should be generating attr_accessible with scaffolds. what happens if you generate a model?

Still highly recommend switching to rails 4. Strong parameters is the only thing that I really noticed.

KoRMaK
Jul 31, 2012



But why does one do it and not the other.

kayakyakr
Feb 16, 2004

Kayak is true

KoRMaK posted:

But why does one do it and not the other.

Can't tell ya. Are you sure that you generated the scaffold with attributes? It's part of the rails 3.2 generator natively, so if you aren't seeing it... run:

code:
rails -v
and make sure you are actually using rails 3.2 like you think you are.

Molten Llama
Sep 20, 2006

KoRMaK posted:

But why does one do it and not the other.

Probably some weird thing the SaaS kit is doing, or some other gem you used in the older project.

attr_accessible is older than dirt and only went away in Rails 4. If those declarations are missing from generated scaffolds in a Rails 3.2 project, that project has been reconfigured not to use it.

no_funeral
Jul 4, 2004

why
Does anybody here have experience with ActiveAdmin, specifically activeadmin-mongoid (https://github.com/elia/activeadmin-mongoid)? At my new job I've been put in charge of implementing various admin features(edit/delete a post or user, etc), and I've run into nothing but problems. Even the basic Formtastic operations don't seem to be working, aside from input(non-plural) and I have no idea where to go for help, as the documentation for both ActiveAdmin, and especially activeadmin-mongoid, are extremely sparse. I'd really appreciate anybody with experience giving me a push in the right direction.

Chilled Milk
Jun 22, 2003

No one here is alone,
satellites in every home

Sitting Bull posted:

Does anybody here have experience with ActiveAdmin, specifically activeadmin-mongoid (https://github.com/elia/activeadmin-mongoid)? At my new job I've been put in charge of implementing various admin features(edit/delete a post or user, etc), and I've run into nothing but problems. Even the basic Formtastic operations don't seem to be working, aside from input(non-plural) and I have no idea where to go for help, as the documentation for both ActiveAdmin, and especially activeadmin-mongoid, are extremely sparse. I'd really appreciate anybody with experience giving me a push in the right direction.

Haven't touched much mongoid stuff but I'm fairly up on my AA. I can say off the bat if you're running Rails 4 you'll need to be grabbing the GitHub master branch, no release with it yet. Post your model and AA resource file for it and I'll give it a once over.

no_funeral
Jul 4, 2004

why

The Milkman posted:

Haven't touched much mongoid stuff but I'm fairly up on my AA. I can say off the bat if you're running Rails 4 you'll need to be grabbing the GitHub master branch, no release with it yet. Post your model and AA resource file for it and I'll give it a once over.

May I contact you off site? If so, what would the best method be?

Chilled Milk
Jun 22, 2003

No one here is alone,
satellites in every home

Sitting Bull posted:

May I contact you off site? If so, what would the best method be?

PM or milkohol@gmail

Newbsylberry
Dec 29, 2007

I Swim in drag shorts because I have a SMALL PENIS
I want to have a mailing list that people can sign up for without creating a password, so I can send them information and store a little bit of info about them without a big commitment. I also want a user with a full account that has access to the features of the site, who will receive the same updates, but also other emails. I was thinking of having the mailing list table searched after user creation for duplicate emails addresses and having them erased. Is that the right way of doing this? I'm using rails4 and devise.

Oh My Science
Dec 29, 2008
Why not use something like mailchimp and have multiple mailing lists?

1 mailing list for your newsletter
1 mailing list for your members

I believe you can setup the rules in such a way that when users register they get assigned to both mailing lists avoiding duplicate entries if they had signed up for the newsletter before.

Newbsylberry
Dec 29, 2007

I Swim in drag shorts because I have a SMALL PENIS

Oh My Science posted:

Why not use something like mailchimp and have multiple mailing lists?

1 mailing list for your newsletter
1 mailing list for your members

I believe you can setup the rules in such a way that when users register they get assigned to both mailing lists avoiding duplicate entries if they had signed up for the newsletter before.

The thing is I would like to be able to send people who register for the mailing list account emails based on certain criteria, like if they are matched with another user, as a way to get them to take the next step to fully register.

kayakyakr
Feb 16, 2004

Kayak is true
Could make the unregistered users into actual users, give them a random password, save an "unregistered" state on the user model, and prompt them to fully register if you need?

Molten Llama
Sep 20, 2006

Newbsylberry posted:

The thing is I would like to be able to send people who register for the mailing list account emails based on certain criteria, like if they are matched with another user, as a way to get them to take the next step to fully register.

I'm not totally clear on how you need this to work, but MailChimp allows you to store metadata if that would be helpful for your use case.

Fillerbunny
Jul 25, 2002

so confused.
I'm starting work on a project, and am considering building it on Heroku for visibility purposes and porting it to the customer's server once it's accepted/completed. Is this an okay idea? Is there anything I should know up front?

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
The cost. Heroku ain't cheap.

kayakyakr
Feb 16, 2004

Kayak is true

Fillerbunny posted:

I'm starting work on a project, and am considering building it on Heroku for visibility purposes and porting it to the customer's server once it's accepted/completed. Is this an okay idea? Is there anything I should know up front?

If you're able to run it on the heroku freebie server for development, then you should be fine. If you need more server or background tasks or more database than you can get for free, I'd suggest capistrano + either the AWS free tier EC2 server or a $5 digital ocean server. That'd make it easier to deploy to the customer's server as well, since it'd be set up for that sort of architecture in the first place.

Fillerbunny
Jul 25, 2002

so confused.

Smol posted:

The cost. Heroku ain't cheap.

Yeah, I wasn't planning on using the version that costs money. But you're right, it gets pretty expensive.

kayakyakr posted:

If you're able to run it on the heroku freebie server for development, then you should be fine. If you need more server or background tasks or more database than you can get for free, I'd suggest capistrano + either the AWS free tier EC2 server or a $5 digital ocean server. That'd make it easier to deploy to the customer's server as well, since it'd be set up for that sort of architecture in the first place.

Yeah, that's kind of what I was asking, is deployment from Heroku to a customer-owned Linux server going to cause problems for me? I hadn't heard of the digital ocean server. I'll look into that. I'm shying away from AWS a bit since it requires a card to get set up, and I don't want to accidentally stray into the not-free territory by accident.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Fillerbunny posted:

Yeah, that's kind of what I was asking, is deployment from Heroku to a customer-owned Linux server going to cause problems for me? I hadn't heard of the digital ocean server. I'll look into that. I'm shying away from AWS a bit since it requires a card to get set up, and I don't want to accidentally stray into the not-free territory by accident.

Moving from Heroku to a server you manage yourself isn't going to be harder than moving from no hosting to a server you manage yourself. The difficulty is usually when you do things that don't work on Heroku, like using the filesystem for storage. The practices that work on Heroku end up being practices that work well on other services too.

Molten Llama
Sep 20, 2006

Fillerbunny posted:

I hadn't heard of the digital ocean server. I'll look into that.

DigitalOcean's pretty great for isolated VMs. Two things to be aware of if you need to split services across multiple VMs:

1. Only NYC2 instances have any form of private or internal network available. If you don't want to use NYC2 (or can't get into NYC2—it may be at capacity), you'll definitely want to configure your firewall and any authentication option your services offer. Note that this also means all transit between non-NYC2 instances is billable. If you're moving a lot of data between servers, DigitalOcean may not be as cheap as it initially seems. (You've got a TB to play with even on the cheapest plan but some workloads will chew through that quickly.)

2. Their images are nonstandard in subtle ways. If you're using scripts to bring up servers, either do it manually the first time or pay close attention to any logging output. In general there shouldn't be too many issues. Of particular interest: DigitalOcean images start with a completely empty iptables chain. If your deployment scripts expect the distro vendor's chains to be there, you'll probably end up locked out and have to use the console.

kayakyakr
Feb 16, 2004

Kayak is true

Molten Llama posted:

DigitalOcean's pretty great for isolated VMs. Two things to be aware of if you need to split services across multiple VMs:

1. Only NYC2 instances have any form of private or internal network available. If you don't want to use NYC2 (or can't get into NYC2—it may be at capacity), you'll definitely want to configure your firewall and any authentication option your services offer. Note that this also means all transit between non-NYC2 instances is billable. If you're moving a lot of data between servers, DigitalOcean may not be as cheap as it initially seems. (You've got a TB to play with even on the cheapest plan but some workloads will chew through that quickly.)

2. Their images are nonstandard in subtle ways. If you're using scripts to bring up servers, either do it manually the first time or pay close attention to any logging output. In general there shouldn't be too many issues. Of particular interest: DigitalOcean images start with a completely empty iptables chain. If your deployment scripts expect the distro vendor's chains to be there, you'll probably end up locked out and have to use the console.

Yeah, I'm a huge fan of the private network instances now. I've been using ubuntu which is a fairly standard root-only server. setting them up goes:

1. Set up user, authorized keys, sudo for that user
2. dist-upgrade, install ufw, vim, other tools needed
3. enable ufw

Takes about 15 minutes now that I know what I'm doing. Could script it out pretty easily.

Adbot
ADBOT LOVES YOU

Newbsylberry
Dec 29, 2007

I Swim in drag shorts because I have a SMALL PENIS
MailChimp looks like a good way to go! Thanks for the help.

On to the next issue, I am using devise and assigning a role to the user immediately when they sign up. I want to have a select_tag that gives the two role options 'student' and 'professional' since those are the only two roles I want people to be able to give themselves. Unfortunately devise tells me:


NoMethodError in Devise::Registrations#new

undefined method `select_tag' for #<ActionView::Helpers::FormBuilder:0x007fbbd415e608>

Here's the part of the code from the viewer that is giving me trouble:

code:
  <div><%= f.label :role %><br />
    <%= f.select_tag(:role, options_for_select('professional', 'student'))  %>
  </div>
  <br />

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