Archiv für die Kategorie ‘Ruby on Rails’

h1

ActiveRecord::Errors.new(self) and undefined method self_and_descendants_from_active_record

September 9, 2009

We recently switched to rails 2.3.4 and noticed the following error:

undefined method self_and_descendants_from_active_record for Cart:Class

Our Cart class has no ActiveRecord binding, but we need some validations, so we simple use

@errors = ActiveRecord::Errors.new(self)

and overerride the valid? action.

But now we get the undefined method error.

Solution

Implement in your class also:

 def self.self_and_descendants_from_active_record
    [self]
  end

  def self.human_name(options = {})
    defaults = self_and_descendants_from_active_record.map do |klass|
      :"#{klass.name.underscore}"
    end
    defaults < [:activerecord, :models], :count => 1, :default => defaults}.merge(options))
  end

  def self.human_attribute_name(attribute_key_name, options = {})
    defaults = self_and_descendants_from_active_record.map do |klass|
      :"#{klass.name.underscore}.#{attribute_key_name}"
    end
    defaults << options[:default] if options[:default]
    defaults.flatten!
    defaults < defaults, :scope => [:activerecord, :attributes]))
  end

and you can still use ActiveModel like validations :)

h1

javascript value and rails I18n

September 8, 2009

Today i stumble about the following problem, i have a javascript value and want it inside a localized rails string.

„This is my string javascriptvalue and so on …“

solution

'<%= T("This is my string ## and so on ...", :translate_string) %>'.sub(/##/, javascript_value);

i simple use javascript sub to replace ## with the javascript_value.

:)

h1

Switch to Mac

Mai 9, 2009

Vor einer Woche habe auch ich den Schritt zu Apple gewagt und mir ein „generalüberholtes“ MacBook geleistet.

Im Moment bin ich noch am einrichten / konfigurieren / Programme suchen, aber alles in allem ist es doch schön eine voll funktionsfähige Konsole zur Verfügung zu haben und den komfort einer schönen Oberfläche.

Allen Rails Entwicklern sei der folgende Artikel ans Herz gelegt:
http://giantrobots.thoughtbot.com/2009/3/30/2009-rubyist-guide-mac-os-x-development-environment

(Achtung: MySQL auch wirklich in der 32 Bit Version installieren!)

Als IDE empfehle ich Netbeans in der Version 6.7, auch wenn sich diese noch im Beta Stadium befindet.

So, und nun begebe ich mich noch auf die Suche nach einem einfachen Bildbearbeitungsprogramm ;-)

h1

CentOS and mod_rails – failed to map segment from shared object: Permission denied

April 6, 2009

After some playing with VirtualBox and CentOS i try to install mod_rails, all in all no big deal, but if i try to start / restart httpd service i got:

Stopping httpd: [FAILED]
Starting httpd: httpd: Syntax error on line 1of /etc/httpd/conf/
rails.conf: Cannot load /opt/ruby-enterprise/lib/ruby/gems/1.8/gems/
passenger-2.1.2/ext/apache2/mod_passenger.so into server: /opt/ruby-
enterprise/lib/ruby/gems/1.8/gems/passenger-2.1.2/ext/apache2/
mod_passenger.so: failed to map segment from shared object: Permission
denied [FAILED]

Solution:
Disable SELinux, or have a closer look to the docs.

h1

sortable_element with table

März 24, 2009
<table class="listing">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="project_versions">
<% for item in project_versions %>
  <% content_tag_for(:tr, item) do %>
<td class="drag_handle">[drag]</td>
<td><%= h item.name -%></span>
  <% end %>
<% end %>

 <%= sortable_element('project_versions', :url => {:controller => 'project', :action => 'sort_project_version'}, :handle => :drag_handle, :tag => :tr) %>

Without many words, i hope the code speaks for itself.
Take care of the :tag attriubte in the helper (default is li) and the container has to be the tbody.

h1

Selling Rails in a Recession

März 5, 2009

According to this grailsblog post, here is the Rails port:

Open Source Stack = $0

Because Rails runs on the JVM (JRuby), it is platform agnostic and capable of running on top of a 100% open source stack. Everything including the OS, app server, and database may be obtained free of charge with no license fees.

Faster Development Time

Yes, it’s great that everything needed to run Rails’ applications may be obtained for free, but what’s even a bigger cost savings is how much time developers can save by working within the JRuby/Rails environment. Rails really tends to speed up development in the following 3 areas:

Data Access

ActiveRecord provides an amazingly efficient way to model domain objects as well as persist them to a database.

Scaffolding

Scaffolding speeds up development time rapidly.

Syntax

When working with JRuby and Rails, developers will find themselves writing less code than when working within the traditional Java environment. Less code translates into shorter development time.

Summary

Rails is a great fit for a recession. It allows developers to create high quality applications on top of a free open source stack in less time than many of the more common, corporate frameworks.

h1

CentOS + VirtualBox + Ruby on Rails

Februar 7, 2009

Just some noticable Infos:

I installed VirtualBox (virtualbox.org) and setup CentOS, for this i downloaded the complete image at http://virtualbox.wordpress.com/images/centos/ (remember to copy the root password for the first login)

To start setup:
- change root password
- add an additional user
- update system files (yum update)
- install mysql (to use with ruby mysql gem you need to install also the developer packages)
- install ruby-enterprise (setup .bashrc)
- install subversion (yum install subversion)

Some useful infos:

Install Ruby Enterprise (including Rails):
Installing Ruby Enterprise on CentOS 5.2

Install MySQL Gem
Faild for me, so i found here the solution:
mysql gem on centos
Update: More Infos: Mysql gem centos5 hell

German Keyboard Layout
To change temporary (until reboot)
loadkeys de-latin1
And to verify that the keyboard layout is correct use the command: showkey

Permanent setting (CentOS):
Edit /etc/sysconfig/keyboard and replace KEYTABLE=“us“ by KEYTABLE=“de-latin1″
save and reboot.

To find whitch keyboard layout maps that is available is in the map
/lib/kbd/keymaps/i386/…depending on which distro is used.

Enable high resolution console:
As root, edit the file ‘/etc/grub.conf’, and add ‘vga=XXX’ to the line that begins with kernel.
Get other values here: VGA Screen Resolution

I will update this post, but for now i dont remember more steps ;)

h1

Time and MSSQL

August 7, 2008

Today i want to blog about an issue we noticed few days ago.

We use MSSQL DB for our Ruby on Rails Webapplication. We got a lot of data replicated from other systems, so we also got created_at and updated_at columns filled.

After successfull replication we just clicking through the application and wonder why the created_at and updated_at data is not displaying.

The created_at / updated_at data in the database looks like ‘2008-07-18 00:00:00′.
Some playing shows us if we add 1 second to the Date it was showing up.

Customer.all.collect {|c| c.update_attributes(:created_at => Time.parse(c.created_at_before_type_cast)+1.second)}

Strange… it seems that mssql and jruby have a problem with datetime columns where time is 00:00:00.

Im going to update this post if any solution is found, or posted by you in the comments.

h1

Background-FU und die daemons

August 6, 2008

In einer meiner Applikationen möchte ich Hintergrundprozesse für regelmäßig anfallende Aufgaben benutzen.

Im wesentlichen gibt es dafür folgende Plugins / Gems:

  • Daemons
  • BackgrounDRb
  • Background-FU

und bestimmt noch einige mehr.

Da mein Prozess lediglich eine Klasse ausführen soll die das Ergebnis in die Datebank speichert, möchte ich background_fu verwenden.

Im Moment sieht es leider danach aus als das ich background_fu doch nicht nutzen kann, da das benötigte daemons plugin nicht mit Windows funktioniert. (Bzw. hab ich noch nicht rausgefunden wie ich es zum laufen bekomme :) )

h1

Ruby on Rails und das Datenbank Disaster

August 4, 2008

In meinem ersten Blog Eintrag möchte ich meine leidvollen Erfahrungen in Bezug auf Ruby on Rails und eine zerstörte Datenbank dokumentieren. (Naja, zerstört ist übertrieben, aber mir kam es so vor.)

Als relatvier RoR Neuling (dabei seit 1.5.2008 ) ist jede Erfahrung wichtig. Aber diese hat mir einige schweisstreibende Stunden eingebracht.
Alles fing damit an das es an der Zeit war neben der Entwicklungsumgebung auch eine Produktiv Umgebung einzurichten. Soweit so gut, also mal eben eine Datenbank geordert (mssql), dann rake db:migrate ausgeführt fertig. Vorerst…

Read the rest of this entry ?