我有连接到多个数据库的rails应用程序.我写了自定义rake任务,看起来像这样:
task :migrate_accounts_schema => [:environment] do |t|
users = User.find :all, :conditions => ["state = 2"], :order => "id asc"
users.each do |user|
if user.state == 2
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => user.database_host,
:port => user.database_port,
:username => user.subdomain,
:password => "#{user.database_password}",
:database => user.database_name
)
Rake::Task["db:migrate"].invoke
end
end
end
Run Code Online (Sandbox Code Playgroud)
问题是任务执行db:仅针对users [0]用户(集合中的第一个用户)进行迁移并且没有错误,只是静默地停止...
这是rake --trace的输出
** Invoke app:migrate_accounts_schema (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute app:migrate_accounts_schema
** Invoke db:migrate (first_time)
** Invoke environment
** Execute db:migrate
** …Run Code Online (Sandbox Code Playgroud) 我想创建此查询:
select * from products where number in ('123', '234', '456');
Run Code Online (Sandbox Code Playgroud)
但我找不到任何使用Npgsql和NpgsqlParameter实现此功能的示例.我试过这样的:
string[] numbers = new string[] { "123", "234" };
NpgsqlCommands cmd = new NpgsqlCommands("select * from products where number in (:numbers)");
NpgsqlParameter p = new NpgsqlParameter("numbers", numbers);
command.Parameters.Add(p);
Run Code Online (Sandbox Code Playgroud)
但它没有用;)
我写了自己的发电机,从这个发射的控制台开始
rails generate ead_document TechnicalOpinion --document_type_id=1
Run Code Online (Sandbox Code Playgroud)
它创建模型和迁移.我想从我的控制器执行生成器而不使用ruby系统命令.有没有办法做到这一点?
我将Nhibernate和log4net配置为记录由nhibernate执行的查询.有没有办法记录每个查询执行时间?
我需要能够旋转标签中的文本并将其与左,右或中心对齐.到目前为止,我能够在派生标签的onPaint方法中使用此代码进行旋转:
float width = graphics.MeasureString(Text, this.Font).Width;
float height = graphics.MeasureString(Text, this.Font).Height;
double angle = (_rotationAngle / 180) * Math.PI;
graphics.TranslateTransform(
(ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle))) / 2,
(ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle))) / 2);
graphics.RotateTransform(270f);
graphics.DrawString(Text, this.Font, textBrush, new PointF(0,0), stringFormat);
graphics.ResetTransform();
Run Code Online (Sandbox Code Playgroud)
它工作正常.我可以看到文字旋转了270度.
但是当我尝试在stringFormat中设置对齐时,它变得疯狂,我无法弄清楚发生了什么.
如何将文本旋转270度并将其对齐?
我正在尝试将我的应用程序从Rails 3.0.7迁移到Rails 3.1.3.我有客户端模型
class Client::Client < ActiveRecord::Base
has_one :contact_address, :class_name => "Address", :foreign_key => :client_id, :conditions => ["kind = ? and state = ?", 2, 1]
end
Run Code Online (Sandbox Code Playgroud)
在控制器的编辑方法中,我执行以下代码:
def edit
@client = params[:type].classify.constantize.find params[:id]
@client.contact_address = Address.new(:kind => 2) if @client.contact_address.blank?
end
Run Code Online (Sandbox Code Playgroud)
在这段代码的第二行,我收到错误:
Failed to save the new associated contact_address.
Run Code Online (Sandbox Code Playgroud)
所以似乎@ client.contact_address的赋值以某种方式在contact_address对象上触发了save方法......我不希望这样......这是一些新的Rails 3.1.x行为吗?我希望只有在我调用.save时才能保存相关对象!在父模型上 - 这对我来说太神奇了.我可以在某处禁用此行为吗?
我想将文本框绑定到单个DataRow对象(传递给对话框表单进行编辑).这是我的代码:
DataRow row = myDataTable.NewRow();
EditForm form = new EditForm(row);
//in EditForm constructor
nameTextBox.DataBindings.Add("Text", row, "name");
Run Code Online (Sandbox Code Playgroud)
我得到一个错误:无法绑定到DataSource中的属性或列.你知道我错过了什么或者可能有任何变通方法吗?
[添加]
我的DataTable肯定包含DataName = ColumnName ="name"的DataColumn.这是我创建DataTable的代码
public DataTable SelectReturnDataTable(string tableName, string sql, params SQLiteParameter[] parameters)
{
using (SQLiteConnection conn = new SQLiteConnection(_connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
cmd.CommandText = sql;
foreach (SQLiteParameter p in parameters)
cmd.Parameters.Add(p);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable(tableName);
conn.Open();
da.Fill(dt);
return dt;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有模型用户的Rails3应用程序和字段expires_at创建如下:
t.column :expires_at, :timestamp
Run Code Online (Sandbox Code Playgroud)
在我的数据库(postgresql)中它有类型:
timestamp without timezone
Run Code Online (Sandbox Code Playgroud)
问题是我打电话的时候:
@user.expires_at = Time.now
@user.save
Run Code Online (Sandbox Code Playgroud)
它使用UTC时区保存到数据库中(我的当地时间是UTC + 1:00,华沙),但我不希望这样.我只想把我的本地时区保存到数据库中(2011-03-30 01:29:01.766709,而不是2011-03-29 23:29:01.766709)
我可以使用rails3实现这一目标吗?
我将ListBox数据绑定到用户列表(集合):
usersListBox.DataSource = null;
usersListBox.DataSource = _users;
usersListBox.DisplayMember = "Name";
usersListBox.ValueMember = "Id";
Run Code Online (Sandbox Code Playgroud)
现在我希望所选项目的属性显示在不同的文本框中,所以我进行绑定:
nameTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Name");
loginTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Login");
Run Code Online (Sandbox Code Playgroud)
当表单加载时,我可以看到所选项目的值出现在文本框中,但是当列表框中的选定项目发生更改时,文本框中的值仍然相同.我是否必须捕获列表框的selectedItemChanged并重复绑定文本框?或者我遗漏了一些东西,文本框中的值应该随着所选项目的变化而改变?
c# ×5
ruby ×3
data-binding ×2
activerecord ×1
ado.net ×1
drawstring ×1
generator ×1
listbox ×1
log4net ×1
nhibernate ×1
npgsql ×1
postgresql ×1
rake ×1
rotation ×1
textbox ×1
timezone ×1
winforms ×1
yard ×1