我的一个模型有以下关系:
class User(Base):
account = relationship("Account")
Run Code Online (Sandbox Code Playgroud)
我想手动设置帐户ID.
我的第一次尝试是这样的:
class User(Base):
account = relationship("Account")
accounts_id = Column(Integer, ForeignKey("accounts.id"), nullable=True)
@classmethod
def from_json(cls, json):
appointment = Appointment()
appointment.account_id = json["account_id"]
return appointment
Run Code Online (Sandbox Code Playgroud)
以上都不行.我们不能引用此列,因为SQLAlchemy会引发一个拟合.这是例外:
sqlalchemy.exc.InvalidRequestError: Implicitly combining column users.accounts_id with column users.accounts_id under attribute 'accounts_id'. Please configure one or more attributes for these same-named columns explicitly.
Run Code Online (Sandbox Code Playgroud)
我试图通过文档进行搜索,并且通过多种方式获取属性但是我无法找到,更不用说设置它了.
print(self.account.account_id)
print(self.account.relationhip)
print(self.account.properties)
print(self.account.primaryjoin)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
[编辑 - 上面添加的例外]
尝试通过传入类似这样的类来格式化Datetime select helper一直没有用.我起初认为Bootstrap可能是错的,但在查看源代码后,'span2'不存在.
<%= f.datetime_select :arrival_nor_tendered, class: "span2" %>
Run Code Online (Sandbox Code Playgroud)
这似乎是做头条的麻烦,但我可能错了.
我试图使用此命令从命令行打开vim中的多个文件.出于某种原因,我会得到一个错误,你将在下面看到.
$ find . -name edit.html.erb \
| perl -pi -e 's/^..//' \
| perl -pi -e 's/\n$/ /' \
| vim
Run Code Online (Sandbox Code Playgroud)
当我在没有"| vim"的情况下运行它时,如以下命令:
$ find . -name edit.html.erb \
| perl -pi -e 's/^..//' \
| perl -pi -e 's/\n$/ /'
Run Code Online (Sandbox Code Playgroud)
得到这个输出:
addresses/edit.html.erb claims/edit.html.erb emails/edit.html.erb owners/edit.html.erb packages/edit.html.erb users/edit.html.erb vessels/edit.html.erb voyages/edit.html.erb %
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,可能与输出结尾处的"%"标记有关.
这是输出和错误:
Vim: Warning: Input is not from a terminal
Vim: Error reading input, exiting...
Vim: preserving files...
Vim: Finished.
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?不知道哪个部分导致错误,因为我是一个新手,但任何帮助将不胜感激!
我想在我的Rails应用程序中使用我的YAML解析器来告诉我它在哪里绊倒.
通常,在将大量内容添加到YAML文件后,我在rails应用程序中收到此错误:
Can not load translation from /path/to/rails/app/config/locales/en.yml, expected it to return a hash, but does not
Run Code Online (Sandbox Code Playgroud)
我想知道如何调试这个错误,以便它可以告诉我一条关于我失败的地方.现在我的策略是查看文件并试图找出错误.
通过应用程序的跟踪不再具有描述性,只告诉我YAML解析器正在运行.
解析器是否可以告诉它在哪里窒息?
(我是新手,所以我将发布我的YAML文件的样本)
sp:
static_pages:
ship_agency_title: Agenciamiento de Barco
ship_agency_summary: |
Mardom ofrece servicios de agenciamiento a diferentes líneas marítimas, entre las cuales están líneas con servicios especializados en el transporte de carga contenerizada.
project_cargo_handling_title: Manejo de Proyectos
project_cargo_hangling_description: |
Mardom ofrece un servicio especializado en el manejo de cargas con sobre-dimensión y/o sobrepeso (carga de proyectos).
ship_and_cargo_brokerage_title: Fletamento de Buques y …Run Code Online (Sandbox Code Playgroud) 我将一些旧的Omnis 7数据库中的数据通过文本文件导入Rails.我正在使用文本文件,并使用db:seed将其移植到.
在我的seeds.rb文件中,我有:
Port.delete_all
File.open("db_ports.txt", "r") do |ports|
ports.read.each_line do |port|
id, name, un_locode, bunker_surcharge, launch_tariff = port.chomp.split("|")
Port.create!(:id => id, :name => name, :un_locode => un_locode, :bunker_surcharge => bunker_surcharge, :launch_tariff => launch_tariff)
end
end
Run Code Online (Sandbox Code Playgroud)
我希望ID号是在另一个数据库中分配的ID号,因为Berths有一个外键,它看起来像Ports,我需要那些相互匹配.
但是,当我上次导入我的数据时,这没有按计划运行,并且没有传入ID号.我猜我可以创建空记录,然后我会用数据更新但是想知道这是不是可能.
我试图在我的所有Rails模型上运行快速rake任务,但无法调用它们,因为这段代码告诉我我不能columns在字符串上调用该方法.
我试过classify而不是camelize它也没有工作,尝试class_eval在那里插入一个,但这似乎在这里工作/不太了解它.
task :collect_models_and_field_names => :environment do
models = Dir.glob("#{models_path}/*").map do |m|
m.capitalize.camelize.columns.each { |n| puts n.name }
end
Run Code Online (Sandbox Code Playgroud)
我知道这样做有效,所以如果需要,我可以手动访问模型,但我真的不想这样做......
Model.columns.each { |c| puts c.name }
Run Code Online (Sandbox Code Playgroud) 你可以按功能更改MySQL吗?我试图按照任意顺序对我的值进行排序.
目前正在寻找注入函数的方法,这可能会帮助我在这里添加一个列并修改导入.
这是我想要的顺序:
AAA
AA+
AA
AA-
A+
A
A-
BBB+
BBB
BBB-
BB+
BB
BB-
B+
B
B-
CCC+
CCC
CCC-
CC
Run Code Online (Sandbox Code Playgroud)
这是我使用sort by的结果:
A
A+
A-
AA
AA+
AA-
AAA
B
B+
B-
BB
BB+
BB-
BBB
BBB+
BBB-
C
CC
CCC
CCC+
CCC-
Run Code Online (Sandbox Code Playgroud)
编辑:尝试但得到语法错误:
CREATE FUNCTION sortRating (s CHAR(20))
RETURNS INT(2)
DECLARE var INT
CASE s
WHEN 'AAA' THEN SET var = 1
WHEN 'AA+' THEN SET var = 2
ELSE
SET VAR = 3
END CASE …Run Code Online (Sandbox Code Playgroud) 所以我正在运行这个班轮:
perl -i -pe 's|[^\d\n]||g' country-ids.txt
Run Code Online (Sandbox Code Playgroud)
要替换如下所示的文件中的文本:
# encoding: utf-8
files_to_change = ["db_owners.txt", "db_vessels.txt"]
files_to_change.each do |file|
text = File.read(file)
,"1")
,"1")
,"2")
,"2")
,"3")
Run Code Online (Sandbox Code Playgroud)
我的目标是剥离所有非数字字符的每一行,同时将线保持在原始位置.
而不是我想要的结果,我得到这个:
d
dd
d
d
<blank space>
Run Code Online (Sandbox Code Playgroud)
不知道发生了什么事.我保留新行的原因是因为我最后一次运行它而没有"查找"字符类中的换行符所有我得到的是一个非常非常长的数字行.
我使用%x {command}在.rb脚本中运行它,如果这有任何区别的话.
编辑:
这是整个脚本.仍然遇到同样的问题.不知道为什么.
%x{cut -f 2 -d/ script-substitute-countries-with-id.rb > countries2.txt}
%x{cut -f 2 db_vessels.txt > countries.txt}
%x{cut -f 3 -d/ script-substitute-countries-with-id.rb > country-ids.txt}
%x{perl -i -pe 's:[^\d]::g' country-ids.txt}
%x{join countries2.txt country-ids.txt > countries2.txt.tmp}
%x{mv countries2.txt.tmp countries2.txt}
%x{cat countries.txt countries2.txt > countries.txt}
%x{uniq countries.txt > countries.txt.tmp} …Run Code Online (Sandbox Code Playgroud) 播种数据时,我有时会收到如下错误:
Owner(#70279918514200) expected, got String(#70279913263500)
Run Code Online (Sandbox Code Playgroud)
Rake有可能告诉我它窒息的记录是什么?
编辑:
这有关于我正在寻找的更多信息:
我有一个平面文件,表明我需要的数据.此文件是从未经验证的数据库创建的,该数据库具有许多不同的拼写,例如Proctor(和| y | avec | gen)Gamble.我已经能够翻译几乎所有这些,但有一些已经滑过裂缝仍然给我带来麻烦.我可以在运行时从rake看到平面文件的哪一行
flat_file.each_line
Run Code Online (Sandbox Code Playgroud)
造成麻烦?
所以我真的很喜欢Lisp中的这种语法:
(+ 1 1 2 3 5 8 13)
=> 33
Run Code Online (Sandbox Code Playgroud)
我想在Ruby中添加一个项目列表,并希望尽可能地近似.现在,我最好的解决方案涉及一个数组和collect/map方法.
所以:
sum = 0; [1,1,2,3,5,8,13].collect { |n| sum += n }
Run Code Online (Sandbox Code Playgroud)
但...
我想为此添加可返回nil的方法.
sum = 0; [1, booking_fee, 8,13].collect { |n| n = 0 if n.nil?; sum += n }
Run Code Online (Sandbox Code Playgroud)
这样做真的很好,中间的所有行都引用可能返回nil的方法,但我不能以这种方式完全构建数组.这只是我希望我的语法看起来像什么的想法.
def total
Array.new do
booking_fee
rental_charges
internationalization_charges
discounts
wild_nights
end.collect { |n| n = 0 if n.nil?; sum += n }
end
Run Code Online (Sandbox Code Playgroud)
在我试图破解并实现格林斯普规则之前的任何建议?(编程确实是一种强制性.