Say you have an ActiveRecord model with a custom primary key and no field named “id”, a common occurrence when working with legacy data:
hotel.rb
class Hotel < ActiveRecord::Base
set_primary_key :hotel_id(…)
Now you have the pertinent form
<% form_for(@hotel) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label ‘Hotel ID’ %>:
<%= f.text_field :hotel_id %>
</p>
<p>
<%= f.label ‘Name’ %>:
<%= f.text_field :name %>
</p>
<p>
<%= f.label ‘Destination ID’ %>:
<%= f.text_field :destination_id %>
</p><p>
<%= f.submit ‘Save’ %>
</p>
<% end %>
and default controller code:
hotels_controller.rb
def create
@hotel = Hotel.new(params[:hotel])respond_to do |format|
if @hotel.save
(…)
However, this won’t work. The mass assignment at
@hotel = Hotel.new(params[:hotel])
can’t be used with a “custom” primary key as this code called by the initializer excludes the primary key:
active_record/base.rb
def attributes_from_column_definition
self.class.columns.inject({}) do |attributes, column|
attributes[column.name] = column.default unless column.name == self.class.primary_key
attributes
end
end
It is therefore necessary to call the initializer (Hotel.new) without the params hash and set the attributes manually:
hotel_controller.rb
def create
@hotel = Hotel.new
my_params = params[:hotel]
@hotel.hotel_id = my_params[:hotel_id]
@hotel.name_english = my_params[:name]
@hotel.destination_id = my_params[:destination_id]respond_to do |format|
if @hotel.save(…)
If you get errors such as
fatal: Out of memory, malloc failed
error: pack-objects died with strange error
error: failed to push some refs to ‘git@myserver.com:foo.git’
when running
git push
first, upgrade git to the latest version on both ends, then check if
git repack
still works. If not, you might have a local issue.
If repack still works, your remote server (the server you push to) might actually run out of memory, which can easily happen with for example smallish VPS slices. So, just increase your swap file as Linus suggests, like this and it might start working again. Hope it helps.
Mozilla Thunderbird does not have a built-in option to right-click and forward multiple messages without sending them as attachments of a single message or sending each message inline after entering the address manually. While this makes sense as it minimizes server resources and web traffic, sometimes it is more convenient for sender to send (bounce) multiple messages to the same recipient in one action, giving the reader single messages in her inbox. Luckily there’s the Mail Redirect Add-on that helps with this task, one of the most useful Thunderbird extensions.
This code returns all records of your model sorted by translated field name when using Globalize2 :
#app/models/widget.rb
def self.all_sorted
Widget.find(:all, :joins => :globalize_translations,
:conditions => ['locale = ?', I18n.locale]).sort_by(&:name)end
As outlined it this post, :joins => :globalize_translations is mandatory. The unary ampersand operator converts a Proc to a block, so in this case sort_by(&:name) is “shorthand” for
sort {|x,y| x.name <=> y.name}
Ruby sometimes feels like assembling a Swiss watch in code, where other languages feel like fixing an old car.
Recent Comments