を
1:1:1~2:3~4
ぐらいの比率でまぜて
ダシを適宜加えて、食材に和える
詳しいレシピは以下を参照してください。
>> aaa = "User" => "User" >> ccc = aaa.constantize.new => #<User id: nil, name: nil, created_at: nil, updated_at: nil>こんな感じでcccにUserクラスをnewしたオブジェクトが格納できます。
class User < ActiveRecord::Base
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :users
def to_label
"#{code}:#{name}"
end
end
| id | group_id | name |
|---|---|---|
| 1 | 1 | aaa |
| 2 | 1 | bbb |
| 3 | 3 | ccc |
| id | code | name |
|---|---|---|
| 1 | g1 | xxx |
| 2 | g2 | yyy |
class UsersController < ApplicationController
active_scaffold :user do |config|
config.columns = [:id, :group, :name]
config.columns[:group].form_ui = :select
end
end
| id | group | name |
|---|---|---|
| 1 | g1:xxx | aaa |
| 2 | g1:xxx | bbb |
| 2 | g2:yyy | ccc |
| id | group | name |
|---|---|---|
| 2 | g2:yyy | ccc |
| 1 | g1:xxx | aaa |
| 2 | g1:xxx | bbb |
class UsersController < ApplicationController
active_scaffold :user do |config|
config.columns = [:id, :group, :name]
config.columns[:group].form_ui = :select
config.columns[:group].includes = [:group]
config.columns[:group].sort_by :sql => "groups.code"
config.list.sorting = [{:group => :desc},{:name => :asc}]
end
end
class User < ActiveRecord::Base has_and_belongs_to_many :items end class Item < ActiveRecord::Base has_and_belongs_to_many :users end商品を一つづつしか買えないのならいいですが、たいてい複数個買えたりすると思います。 多対多の関係に何個買ったかという属性をつけたいと思ったわけです。 そんな時は、 has_many :through を使えばよかったことに今更に気づいてみました。 人と商品の関係を注文として定義することにします。
class User < ActiveRecord::Base has_many :orders has_many :items, :through => :orders end class Order < ActiveRecord::Base belongs_to :user belongs_to :item end class Item < ActiveRecord::Base has_many :orders has_many :users, :through => :orders endこれでOrderモデルに何個買ったかを定義させることができるというわけです。 このとき、うっかり has_many :orders を書き忘れて
class User < ActiveRecord::Base has_many :items, :through => :orders end class Order < ActiveRecord::Base belongs_to :user belongs_to :item end class Item < ActiveRecord::Base has_many :users, :through => :orders endと書いてしまうと ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association という感じのエラーが出たりします。 以下のサイトを参考にさせていただきました。 http://wiki.usagee.co.jp/ruby/rails/RailsGuides%E3%82%92%E3%82%86%E3%81%A3%E3%81%8F%E3%82%8A%E5%92%8C%E8%A8%B3%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F%E3%82%88/Active%20Record%20Associations#v41bb3f5
CalendarDateSelect::FORMATS[:japanese] = {
:date => "%Y/%m/%d",
:time => " %H:%M",
:javascript_include => "format_japanese"
}
CalendarDateSelect.format = :japanese
Date.prototype.getAMPMHour = function() { hour=Date.padded2(this.
getHours()); return (hour == null) ? 00 : (hour > 24 ? hour - 24 : hour
) }
Date.prototype.getAMPM = function() { return (this.getHours() < 12) ? ""
: ""; }
Date.prototype.toFormattedString = function(include_time){
str = this.getFullYear() + '/' + Date.padded2(this.getMonth() + 1) + '
/' +Date.padded2(this.getDate());
if (include_time) { hour=this.getHours(); str += " " + this.
getAMPMHour() + ":" + this.getPaddedMinutes() }
return str;
}
Date.parseFormattedString = function (string) {
var regexp = "([0-9]{4})(\/([0-9]{2})(\/([0-9]{2})" +
"( ([0-9]{1,2}):([0-9]{2})" +
"?)?)?)?";
var d = string.match(new RegExp(regexp, "i"));
if (d==null) {
return Date.parse(string); // Give javascript a chance to parse it.
}
var date = new Date(d[1], 0, 1);
if (d[3]) { date.setMonth(d[3] - 1); }
if (d[5]) { date.setDate(d[5]); }
if (d[7]) {
hours = parseInt(d[7], 10);
date.setHours(hours);
}
if (d[8]) { date.setMinutes(d[8]); }
return date;
}
これは、
http://code.google.com/p/calendardateselect/wiki/ChangingDateFormatCustom
とか
http://blog.champierre.com/archives/822
とかを参考にさせていただきました。
あとは、カレンダーの曜日とかの表示を日本語にしたり、calendar_date_selectで使えるオプションのforceとかをActiveScaffoldでも使えるようにしたいなぁと思っていますが、まだやり方不明です。
_translations = {
"OK": "OK",
"Now": "現在",
"Today": "今日",
"Clear": "閉じる"
}
Date.weekdays = $w("日 月 火 水 木 金 土");
Date.months = $w("1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月" );
<%= active_scaffold_includes %> <%= calendar_date_select_includes :locale => "ja" %>ポイントは、active_scaffold_includesの後にcalendar_date_select_includesすることです。
config.columns[:opened_at].options = {:popup => "force"}