我之前创建了仅限本地主机的 RoR 应用程序,其中模型是手动创建的,并为它们生成了脚手架,为数据创建了一个漂亮、快速、易于使用的 CRUD 界面。我在 Netbeans 中使用 SQLite 完成了此操作。
现在我有一个带有 MySQL 数据库的服务器,我希望创建一个快速的 CRUD 应用程序,它提供了一种快速简便的方法来查看数据库中的数据并提供一些选项,如编辑/删除。由于我的数据库已经由 MySQL 指定,这似乎打开了大量的蠕虫病毒,与我在 RoR 中尝试使用 Netbeans 开发脚手架生成的 crud web 应用程序相比,这变得不那么直接了。
我研究过转储当前数据库的架构,db:raking 它,然后生成脚手架。这是正确的方法吗?我还阅读了有关 Magic Model Generator 的内容。这是将 MySQL 数据库转换为 RoR 模型格式的最佳方法吗? http://magicmodels.rubyforge.org/magic_model_generator/
我想我想问的是,对于我的数据库结构,RoR 是否适合模拟快速 CRUD 网络应用程序,以便可以对数据执行基本操作?
下面是我的 MySQL 数据库的 schema.rb。 userId和attachmentId之间存在FK关系。
ActiveRecord::Schema.define(:version => 0) do
create_table "attachments", :primary_key => "attachmentId", :force => true do |t|
t.string "attachmentName", :null => false
t.string "fileType", :limit => 30, :null => false
t.binary "content", :null => false
t.string "printCode"
end
create_table "emails", :primary_key => "emailId", :force => true do |t|
t.integer "userId", :limit => 11, :null => false
t.integer "attachmentId", :limit => 11, :null => false
t.string "body", :limit => 1000
t.string "subject"
end
add_index "emails", ["attachmentId"], :name => "attachmentId", :unique => true
add_index "emails", ["userId"], :name => "userId"
create_table "printUsers", :primary_key => "userId", :force => true do |t|
t.string "email", :null => false
end
add_index "printUsers", ["email"], :name => "email", :unique => true
end
最佳答案
似乎有几种方法可以将 Rails 与现有数据库一起使用。参见 Connect rails to existing postgres DB - models not seen by console and controllers ,尤其是它的链接:http://magicmodels.rubyforge.org/magic_model_generator/和 http://blog.aizatto.com/2007/05/21/activerecord-without-rails/
很高兴指出您不需要将 ActiveRecord 与 rails 一起使用。您的模型可以是任何对象,因此使用 sequel 之类的东西可能更容易或 arel访问数据。
另一种方法是将数据从现有数据库迁移到您通过搭建模型生成的新数据库。
关于mysql - Ruby on Rails 是否适合我想要实现的目标?为现有的 MySQL 数据库创建 CRUD 脚手架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10308788/