I'm running Rails 4.2.3. I have a model, Volunteer, with a serialized attribute, participated. After object creation, I can't update the participated attribute:
> #volunteer = Volunteer.new( participated: {'citizenship' => '', 'daca' => ''} )
=> #<Volunteer id: 1, participated: {'citizenship' => '', 'daca' => ''}>
> #volunteer.participated['citizenship'] = 'test'
=> {'citizenship' => 'test', 'daca' => ''}
> #volunteer
=> #<Volunteer id: 1, participated: {'citizenship' => 'test', 'daca' => ''}>
> #volunteer.changed?
=> true
> #volunteer.save
=> true
> Volunteer.last
=> #<Volunteer id: 1, participated: {'citizenship' => '', 'daca' => ''}>
As you can see, the database isn't being updated. I was originally running rails 4.2.0. At that time, changed? wasn't even returning true--in this scenario, it was returning false. After some searching, this pull request on GitHub led me to suspect that the issue might be a Rails bug. Updating to rails 4.2.3 fixed the problems with changed?, but the database still isn't updating.
Any suggestions would be greatly appreciated! I've sunk several hours into this so far.
For what its worth:
class Volunteer < ActiveRecord::Base
serialize :participated
end
class CreateVolunteers < ActiveRecord::Migration
def change
create_table :volunteers do |t|
t.text :participated
t.timestamps null: false
end
end
end
Related
UPDATE:
Ah! This fixed it:
stripe_account = Stripe::Account.create(
{
:legal_entity => { :type => "company" },
:country => "US",
:managed => true
}
)
In our Rails4 app we are trying to integrate Stripe. We are building an auction site, and we will use Stripe to both charge customers and then also send the money to the sellers.
We set up the pages that deal with charging, and everything works great on that side. Customers can make a purchase. We auth their cards, and then, when the seller delivers the product, we capture the charge. 100% perfection.
However, I have run into problems when I try to create the managed accounts for the sellers. This was working fine:
stripe_account = Stripe::Account.create(
{
:country => "US",
:managed => true
}
)
But the documentation says that I need to specify whether the seller is an individual or a company, so I did:
stripe_account = Stripe::Account.create(
{
:type => "company",
:country => "US",
:managed => true
}
)
And now I get this error:
Stripe::InvalidRequestError in Supplier::ProfilesController#new
Received unknown parameter: type
If I look here:
https://stripe.com/docs/api#create_account
I see:
type
string
Either “individual” or “company”, for what kind of legal entity the account owner is for
So why is this an error?
UPDATE:
Ah! This fixed it:
stripe_account = Stripe::Account.create(
{
:legal_entity => { :type => "company" },
:country => "US",
:managed => true
}
)
Ah! This fixed it:
stripe_account = Stripe::Account.create(
{
:legal_entity => { :type => "company" },
:country => "US",
:managed => true
}
)
I'm working on an Rails 4.1 engine that handles user uploads of photos and videos. I'm using Mongoid-Paperclip to handle the uploads and Paperclip-av-transcoder to encode the videos into several formats. All files are stored at S3. All of that works fine, but as you can expect, encoding the videos can take quite some time, so the next step is to make that happen in the background. I did some googling and found Delayed_Paperclip that seems to do what I need. After that it seemed that Sidekiq was the best option for handling the background processing.
Now the problem is, I can't make all this work together. Running my unit tests I get NoMethodError: undefined method 'process_in_background' so it seems the problem resides on Delayed_Paperclip, although there is no special setup to it.
This is the model firing the problem
module MyEngine
class Video
include Mongoid::Document
include Mongoid::Paperclip
has_mongoid_attached_file :file,
:path => ':hash.:extension',
:hash_secret => "the-secret",
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:bucket => "my-bucket-#{Rails.env}",
:styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:thumb => { :geometry => "250x187#", :format => 'jpg', :time => 10, :auto_rotate => true }
},
:processors => [:transcoder]
validates_attachment :file, :content_type => { :content_type => ["video/x-flv", "video/mp4", "video/ogg", "video/webm", "video/x-ms-wmv", "video/x-msvideo", "video/quicktime", "video/3gpp"] }
process_in_background :file
end
end
I've tried adding require "delayed_paperclip" to the lib/myengine/myengine.rb file but that didn't help.
Regarding Sidekiq, I have added to the test_helper.rb the following:
require 'sidekiq/testing'
Sidekiq::Testing.inline!
Note that I did not forget to run bundle install and Redis is up and running. I'm using Mongoid, not active record.
What I am doing wrong? Has anyone successfully used this setup? Is there another combination of gems that I should try?
Aditional info:
Delayed_paperclip 2.9.1
Mongoid 4.0.2
Mongoid-Paperclip 0.0.9
Paperclip 4.2.1
Paperclip-av-transcoder 0.6.4
Rails 4.1.9
Sidekiq 3.5.0
I've been digging through the code of delayed_paperclip and it is definitely tied to ActiveRecord so not compatible with Mongoid. I gave a try to mongoid_paperclip_queue but that gem hasn't been updated in 4 years and doesn't seem to work with the current versions of rails/mongoid/paperclip as far as I can tell.
I therefore decided the best way to solve my issue would be to override the code of delayed_paperclip that integrates with ActiveRecord and make it instead work with Mongoid.
This is what I ended up doing, and seems to be working fine so far:
lib/myengine.rb
require "mongoid_paperclip"
require "paperclip/av/transcoder"
require "delayed_paperclip"
require "myengine/engine"
module Myengine
end
DelayedPaperclip::Railtie.class_eval do
initializer 'delayed_paperclip.insert_into_mongoid' do |app|
ActiveSupport.on_load :mongoid do
DelayedPaperclip::Railtie.insert
end
if app.config.respond_to?(:delayed_paperclip_defaults)
DelayedPaperclip.options.merge!(app.config.delayed_paperclip_defaults)
end
end
# Attachment and URL Generator extends Paperclip
def self.insert
Paperclip::Attachment.send(:include, DelayedPaperclip::Attachment)
Paperclip::UrlGenerator.send(:include, DelayedPaperclip::UrlGenerator)
end
end
DelayedPaperclip::InstanceMethods.class_eval do
def enqueue_post_processing_for name
DelayedPaperclip.enqueue(self.class.name, read_attribute(:id).to_s, name.to_sym)
end
end
Then all you need is to include the delayed_paperclip glue to the model:
module Myengine
class Video
include Mongoid::Document
include Mongoid::Paperclip
include DelayedPaperclip::Glue # <---- Include this
has_mongoid_attached_file :file,
:path => ':hash.:extension',
:hash_secret => "the-secret",
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:bucket => "my-bucket-#{Rails.env}",
:styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:thumb => { :geometry => "250x187#", :format => 'jpg', :time => 10, :auto_rotate => true }
},
:processors => [:transcoder]
validates_attachment :file, :content_type => { :content_type => ["video/x-flv", "video/mp4", "video/ogg", "video/webm", "video/x-ms-wmv", "video/x-msvideo", "video/quicktime", "video/3gpp"] }
process_in_background :file
end
end
Hopefully this will save somebody else all the trouble.
Let me explain first my model structure:
i have a status model:
class Status
include Mongoid::Document
include Mongoid::Search
include Mongoid::Timestamps
field :status_code, type: Integer
field :status_description, type: String
validates :status_code, :status_description, :transactiontype, :presence => true
belongs_to :transactiontype, :class_name => 'Transactiontype'
has_many :transactions, :class_name => 'Transaction', autosave: false
search_in :status_code, :status_description, :transactiontype => :transaction
def self.getStatus(transactiontype)
statuses = Status.where(:transactiontype_id => transactiontype).all
stats = []
puts "DATE DASHBOARD: #{Time.now.beginning_of_day} to #{Time.now.end_of_day}"
statuses.each do |status|
transactions = status.transactions.dateRange(Date.today.beginning_of_day, Date.today.end_of_day)
if transactions.length > 0
status.transactions = transactions
stats.push(status)
end
end
puts "SIZE : #{stats.size}"
stats
end
etc..
end
then i have another model called transactions:
class Transaction
include Mongoid::Document
include Mongoid::Search
include Mongoid::Timestamps
field :ref_no, type: String
field :trans_date, type: DateTime
belongs_to :status, :class_name => 'Status'
belongs_to :transactiontype, :class_name => 'Transactiontype'
validates :ref_no, :trans_date, :status, :presence => true
def self.dateRange(startdate,enddate)
puts "DATE : #{startdate} to #{enddate}"
if !startdate.blank?
where(:created_at => {"$gt" => startdate.beginning_of_day, "$lt" => enddate.end_of_day})
# where(:trans_date.gte => startdate.beginning_of_day, :trans_date.lte => enddate.end_of_day)
end
end
etc..
end
the weird part is that:
when im trying to execute:
Status.getStatus(params[:transactiontype_id])
i received the correct output but the transactions associated with the Status is being updated and each records before the filtered date is being updated with null status_id.
i already tried to add autosave: false but nothing works
can someone help me with this?
the solution is to convert the active record to json first
def self.getStatus(transactiontype)
statuses = Status.where(:transactiontype_id => transactiontype).all
stats = []
puts "DATE DASHBOARD: #{Time.now.beginning_of_day} to #{Time.now.end_of_day}"
statuses.each do |status|
ar_status = status.as_json
ar_status['transactions'] = status.transactions.dateRange(Date.today.beginning_of_day, Date.today.end_of_day)
if ar_status['transactions'].length > 0
stats.push(ar_status)
end
end
puts "SIZE : #{stats.size}"
stats
end
for some reason.. its auto saving the records.
When Upgrading rails application from 3.2.17 to 4.0.4, I am getting this errors
default_controller_and_action': 'Sessions' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use (ArgumentError)
In my routes file content
devise_for :users, :controllers => { :sessions => "Sessions", :passwords => "Passwords", :registrations => "registrations" } , :path => '', :path_names => {
:sign_in => 'login',
:sign_out => 'logout'
}
root :to => "children#index"
And SessionsController is extended from devise controller as follow
class SessionsController < Devise::SessionsController
...........................
....................
end
Why I am getting Sessions is not supported controller name ? I have try to change it in routes and controller but still facing same problem.
Solve this issues. This issues was due to case sensitive. We have to use small letter instead of Capital letter. For example, not Sessions but sessions. I was using Sessions in routes file but I have change it as sessions. The above code can written as
devise_for :users, :controllers => { :sessions => "sessions", :passwords => "passwords", :registrations => "registrations" } , :path => '', :path_names => {
:sign_in => 'login',
:sign_out => 'logout'
}
i have an simple setup for a has_many association:
def Account
has_many :timers
end
def Timer
belongs_to :timer
end
if i try to build a timer i get this false behavior:
account.id => 1
account.timers.build => #<Timer id: nil, account_id: 0>
...of course it should be:
account.id => 1
account.timers.build => #<Timer id: nil, account_id: 1>
Because it's rails4 in tried the same with a fresh setup. and it works!
gems: pg, haml-rails, devise, simple_form, globalize3
Question: what could be the cause of breaking the Rails API ?
and the winner is:
gem 'globalize3', github: 'harlock/globalize3', branch: 'rails4-wip'