Rails Dispatch

Rails news delivered fresh

Presented by Engine Yard

Recently asked questions

Vote on questions you’d like to see answered by the expert contributors

Signup to ask questions or vote for the questions you’d like to see answered.

what is the difference between Rails 2 and Rails 3?

Asked March 22, 2012

Hi,

Can somebody help me, im new in ruby on rails. but my boss want to upgrade the version of rails from 2 to 3. i used rails 3.2

This is the situation, There a model Screensaver model, the purpose of this model is to detect the file extension of file to be uploaded. class Screensaver < ActiveRecord::Base
validates_presence_of :filename, :filemessage
validates_numericality_of :fileorder, :duration

class Screensaver < ActiveRecord::Base validates_presence_of :filename, :filemessage validates_numericality_of :fileorder, :duration before_validation :before_validation
  1. Improvement plan
  2. Detect proper video content type.
    validates_inclusion_of :filetype,
    :in => %w( image/jpeg image/gif image/png image/jpg image/pjpeg image/x-png video/vob video/avi video/mpg video/mpeg video/mp4 ),
    :message => “(%s) is not in acceptable format (video/image).”
def uploadedFile=(data) #logger.info “processing upload” if data.size != 0 then self.filename = base_part_of(data.original_filename) self.fileExtension = self.filename.to_s.split(“.”)[self.filename.to_s.split(“.”).size – 1] self.filetype = data.content_type if self.fileExtension == “avi” || self.fileExtension == “vob” || self.fileExtension == “mpg” || self.fileExtension == “mpeg” || self.fileExtension == “mp4”

# avoid duplicate filenames
counter = 0
while File.exist?(“/migrate/upgrade/screensaver/#{self.filename}”)
self.filename = “#{counter}_#{self.filename}”
counter += 1
end

File.open(“/migrate/upgrade/screensaver/#{self.filename}”, “wb”) { |f| f.write(data.read) } else

# avoid duplicate filenames
counter = 0
while File.exist?(“#{Rails.root}/public/screensaver/#{self.filename}”)
self.filename = “#{counter}_#{self.filename}”
counter += 1
end

File.open(“#{Rails.root}/public/screensaver/#{self.filename}”, “wb”) { |f| f.write(data.read) } end end

end def before_destroy delete_files end def delete_files File.delete(“/migrate/upgrade/screensaver/#{self.filename}”) if File.exist?(“/migrate/upgrade/screensaver/#{self.filename}”) && !File.directory?(“/migrate/upgrade/screensaver/#{self.filename}”) File.delete(“#{Rails.root}/public/screensaver/#{self.filename}”) if File.exist?(“#{Rails.root}/public/screensaver/#{self.filename}”) && !File.directory?(“#{Rails.root}/public/screensaver/#{self.filename}”) end def before_validation if (self.fileExtension.nil?) && (self.filename.!nil) self.fileExtension = self.filename.to_s.split(“.”)[self.filename.to_s.split(“.”).size – 1]

end

if (self.fileExtension == “vob” || self.fileExtension == “avi” || self.fileExtension == “mpg” || self.fileExtension == “mpeg” || self.fileExtension == “mp4”) && (self.filename != nil && self.filename != "") self.filetype = “video/#{self.fileExtension}”

end

if self.tenant_id.nil?
self.tenant_id = 0
end
end

def base_part_of(file_name) name = File.basename(file_name) name.gsub(/[^\w._-]/,’’) end def tenant if self.tenant_id.blank?

return nil
else
return Tenant.find(self.tenant_id)
end
end

def self.human_attribute_name(attr) PROPER_TEXT[attr.to_sym] || super end PROPER_TEXT = { :filetype => “File format”, :filename => “File” }

end

when i upload an image, the error i get is no filename
and filetype. Please help me to figure out where and what wrong on that code.
I try to debug that model for three days. Please reply to my question. Really appreciated.

Regards,
Joener

Asked March 29, 2012

I am Ruby on Rails newbie, and I have simple question about what is different between Model.all and Model.order(:name)?
I know both of them are return all records from the Model, and Model.order(:name) is return records with order by name.

The reason that I am asking this question is after I watch http://railscasts.com/episodes/362-exporting-csv-and-excel, and my example does not return column value, it returns each column as object. The only different is my @products = Product.all instead of @products = Product.order(:name).

Thank you in advance for your help.

Asked July 06, 2012

Hi,

Thank you for the article: How Rails 3 Enables More Choices (Part 1), it was quite helpful.

In this article, we can read:

after_initialize: This hook is run after Rails completes its initialization, and all of the user’s initializers have a chance to run. This allows you to run code after initialization but before any requests.

That seems to work quite well. However, it appears that the schema loading also calls the application initialization:

…server$ rake db:schema:load
Application after initialize…
Done
— create_table("my table…

When I load the schema, I’m not expecting the application to be initialized. Or perhaps should I? What I’m looking for is an application startup hook, right after this:

=> Booting WEBrick
=> Rails 3.0.9 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Application after initialize…

Do you know if after_initialize is the property application startup hook I’m looking for?

Best regards,

Patrice

Asked August 22, 2012

how to create roles with admin, Hr and for different departments like open source,dot.net and social media in sign up form and whole thing how to create this pls give me immediately,

Regards,
Vimaa

Asked August 22, 2012

Abheek

I’m new to ruby, and trying to work with the code based shared by another developer who had left us recently.

I understand the standard ruby structure and went around building the application just after I extracted the code base to the ruby structure. When I ran, I face into lot of deprecated issues and errors.

1. is there way i know what version of the ruby, rails , gem used by the application code?
2. how do i get rid of the rake file issues.
3. how do i upgrade to 3 rails in a shot go.

thnaks
selvan
kalai_selvan@hotmail.com

Asked January 25, 2013

11111">%0911111%0A11111%0B11111%0C11111%0D11111%A011111\

Asked January 31, 2013

The attachments API for the new ActionMailer seems a bit awkward.

You’ve changed everything else such that it can be passed as an attribute to the mail() method (or set with the defaults method) ahead of time. There’s a single exposure point for the whole mail API in that single method, and it returns a Mail::Message object—simple, predictable and clean.

But for attachments, you’re calling some magic attachments[] method that somehow guesses which message it’s attaching itself to? It doesn’t go through the mail() method and thus introduces a new exposure point for the mail API.

Now, instead of just worrying about the mail() method and the options it takes, a developer has to worry about other magical methods like attachments[] (are there more magical methods that we don’t know about?) and has to worry about precisely where these methods are called, to make sure they magically attach their payloads to the correct mail message.

The work you’ve done is great and it’s clear that this is a huge step forward for ActionMailer. I think the attachments API could just use a bit more attention and tweaking to make the overall API consistent.

Asked May 11, 2010

Hi Carl,

I really enjoyed your screencast on Arel.

I have a question about polymorphic associations. Does Arel have the ability to create similar functionality to the has_many_polymorphs plugin?

Thanks,
Chris

Asked May 26, 2010

How to run PHP based Expression Engine 2.0 CMS and Rails 3.0 based site on the same domain?

Asked May 02, 2010