ruby-on-rails-3

How to override Rails' default migration generator template

。_饼干妹妹 提交于 2020-01-02 08:22:17
问题 I need to override these migration templates: https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb and https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb inside my rails application so that they pick up the template from rails application instead of the gem itself. I've

Uninitialized content when trying to include ActionController::UrlWriter in model

こ雲淡風輕ζ 提交于 2020-01-02 08:00:11
问题 I'm using Rails 3 beta 4 and trying to include ActionController::UrlWriter in my model, which is the correct way to go about it as far as i can tell, but i get "Uninitialized Constant ActionController::UrlWriter". Any idea why that would be? Did it move in rails 3? 回答1: First I agree with zed. This shouldn't be done in a model. Your models should be unaware of any http url. I do the same in a resque job. Here's what I do : include ActionDispatch::Routing::UrlFor include ActionController:

Rails 3.2.3 MySQL 5.5.1 Mysql2 gem install fails with ld incompatibilities

亡梦爱人 提交于 2020-01-02 07:42:14
问题 I have a clean build system as follows Ubuntu 11.04 Rvm 1.13.5 Ruby 1.9.3p194 Rails 3.2.3 Gem 1.8.24 MySql 5.5.24-1 (installed in /usr/local) I am trying to install the mysql2 gem (0.3.11) and getting the following problem: pal@smurf01:~$ sudo gem install mysql2 [sudo] password for pal: Building native extensions. This could take a while... ERROR: Error installing mysql2: ERROR: Failed to build gem native extension. /usr/local/rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb checking for rb

How to set title for text_field?

旧城冷巷雨未停 提交于 2020-01-02 07:40:11
问题 I know that is something simple, but I'm stuck. I have a several forms in my rails3 app to which I need to add tooltips, by using the tipsy. The text in tooltips should be determined in title\original-title, but I just can't figure it out, how to set it for text_field. <%= f.text_field :what %> gives me <input id="name_which" name="name[which]" size="30" type="text"> and I just can't figure it out, where to put the title text. For example, this one in html works fine <input type="text" name=

Store time interval in PostgreSQL from Rails

依然范特西╮ 提交于 2020-01-02 07:40:11
问题 I need to store time inverval in PosgreSQL. Data will be put from Ruby on Rails. Say, PizzaHut accepts orders from 9:00 to 18:00. I've created following migration: class AddOrderTimeAndDeliveryTimeToMerchants < ActiveRecord::Migration def self.up add_column :merchants, :order_time, :interval end I've read the documentation, and have following code now: Merchant.create( :delivery_time => "9:00 18:00" ) When i execute it, i get following error message: PGError: ERROR: invalid input syntax for

Store time interval in PostgreSQL from Rails

断了今生、忘了曾经 提交于 2020-01-02 07:39:09
问题 I need to store time inverval in PosgreSQL. Data will be put from Ruby on Rails. Say, PizzaHut accepts orders from 9:00 to 18:00. I've created following migration: class AddOrderTimeAndDeliveryTimeToMerchants < ActiveRecord::Migration def self.up add_column :merchants, :order_time, :interval end I've read the documentation, and have following code now: Merchant.create( :delivery_time => "9:00 18:00" ) When i execute it, i get following error message: PGError: ERROR: invalid input syntax for

Rails not picking up custom date & time formats in en.yml

半腔热情 提交于 2020-01-02 07:35:56
问题 I'm not that familiar with I18N in Rails so bear with me. Trying to set a custom date & time format: #config/locales/en.yml en: date: formats: long_dateweek: "%A, %B %d, %Y" time: formats: very_short: "%H:%M" But when I try to use them I just get the default formats: 1.9.3p194 :001 > Time.now.to_date.to_s(:long_dateweek) => "2012-08-22" 1.9.3p194 :002 > Time.now.to_time.to_s(:very_short) => "2012-08-22 16:12:47 -0700" Tried restarting console (and even the server) to no avail... What'd I miss

ActiveRecord query for a count of new users by day

橙三吉。 提交于 2020-01-02 07:22:07
问题 I want to generate a table showing how many users were created each day. What's the most efficient or elegant ActiveRecord query to do this? My (simplified) schema looks like this: create_table "users" do |t| t.datetime "created_at" t.string "name" end I want a table that looks like this, where the integers are a count of new users each day: day new users 2012-04-01 55 2012-04-02 63 2012-04-03 77 2012-04-04 88 I want to generate the table from an array that looks like this: data_table.add

Filter nested model collections in to_json in rails

南笙酒味 提交于 2020-01-02 07:21:09
问题 I have two models: class Calendar < ActiveRecord::Base has_many :events end class Event < ActiveRecord::Base belongs_to :calendar end I need to output all calendars with nested events as json and I can do it like so: Calendar.all.to_json(:include => :events) But I also need to filter events, for example: where(:name => 'bla') How would I do that? So far my solution is manually convert each calendar to hash, filter events and convert them to hash as well, then append events to calendar hash

Help with a Join in Rails 3

こ雲淡風輕ζ 提交于 2020-01-02 07:13:18
问题 I have the following models: class Event < ActiveRecord::Base has_many :action_items end class ActionItem < ActiveRecord::Base belongs_to :event belongs_to :action_item_type end class ActionItemType < ActiveRecord::Base has_many :action_items end And what I want to do is, for a given event, find all the action items that have an action item type with a name of "foo" (for example). So I think the SQL would go something like this: SELECT * FROM action_items a INNER JOIN action_item_types t ON a