Ruby on rails RubyonRails使用名称空间保存记录
我正在使用Rails 5.1构建一个应用程序,我正在尝试在名称空间中组织模型,但是当我尝试保存一个实例时,我得到了一个错误:没有这样的表:main.users也许这里有人可以告诉我方法 app/models/frontend.rb 我正在使用Desive进行用户迁移这是前端::用户模型 app/models/frontend/user.rb 前端迁移::事件模型: 我可以使用has_many:events关系的构建方法Ruby on rails RubyonRails使用名称空间保存记录,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在使用Rails 5.1构建一个应用程序,我正在尝试在名称空间中组织模型,但是当我尝试保存一个实例时,我得到了一个错误:没有这样的表:main.users也许这里有人可以告诉我方法 app/models/frontend.rb 我正在使用Desive进行用户迁移这是前端::用户模型 app/models/frontend/user.rb 前端迁移::事件模型: 我可以使用has_many:events关系的构建方法 e = u.events.build <Frontend::Eve
e = u.events.build
<Frontend::Event id: nil, user_id: 1, title: nil, description:
nil, short_description: nil, start_date: nil, end_date: nil, address:
nil, latitude: nil, longitude: nil, number_of_guest: nil, published:
false, created_at: nil, updated_at: nil>
我认为rails惯例存在一些问题。据我所知,您应该以一种特殊的方式命名联接表 您可以查看rails指南,特别是在部分中查找 按照指南,我可以建造更多更少的你正在建设的东西,但没有设计 在这个例子中,我有三个迁移
class CreateFrontendUsers < ActiveRecord::Migration[5.1]
def change
create_table :frontend_users do |t|
t.string :email
t.timestamps
end
end
end
class CreateFrontendEvents < ActiveRecord::Migration[5.1]
def change
create_table :frontend_events do |t|
t.string :name
t.timestamps
end
end
end
class CreateFrontendEventsUsersJoinTable < ActiveRecord::Migration[5.1]
def change
create_table :frontend_events_users, id: false do |t|
t.integer :event_id
t.integer :user_id
end
add_index :frontend_events_users, :event_id
add_index :frontend_events_users, :user_id
end
end
现在你的模特会像
class Frontend::Event < ApplicationRecord
has_many :event_invitations
has_many :users, through: :event_invitations
end
class Frontend::User < ApplicationRecord
has_many :event_invitations
has_many :events, through: :event_invitations
end
class Frontend::EventInvitation < ApplicationRecord
belongs_to :user
belongs_to :event
end
我总是喜欢用这种联想,而不是用“有很多”和“属于”联想。但你可以决定哪个对你更有利=
有时很难了解所有rails约定,即使您已经使用rails几年了。。。但是,这项研究提供了很多帮助
我希望这会有帮助=当然,谢谢,这在一定程度上起到了作用,但这让工作变得更加困难。我是编程新手,我会坚持约定而不是配置,但我认为这对我来说是有用的,所以我把它放在一边,这样我就可以学习如何在rails应用程序中正确使用名称空间。再次感谢您抽出时间提供帮助。我添加了一个使用has\u many:through association的示例。如果您是Rails领域的新手,也许您可以阅读一下,看看是否更适合您的用例=
class CreateFrontendEvents < ActiveRecord::Migration[5.1]
def change
create_table :frontend_events do |t|
t.references :user, foreign_key: true
t.string :title, :null => false
t.text :description, :null => false
t.string :short_description, :null => false
t.datetime :start_date, :null => true
t.datetime :end_date, :null => true
t.string :address, :null => true
t.float :latitude, :null => true
t.float :longitude, :null => true
t.integer :number_of_guest, :null => true
t.boolean :published, default: false
t.timestamps
end
end
end
u = Frontend::User.create(email: "user@user.com", password: "secret")
e = u.events.build
<Frontend::Event id: nil, user_id: 1, title: nil, description:
nil, short_description: nil, start_date: nil, end_date: nil, address:
nil, latitude: nil, longitude: nil, number_of_guest: nil, published:
false, created_at: nil, updated_at: nil>
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table:
main.users: INSERT INTO "frontend_events" ("user_id",
"created_at","updated_at") VALUES (?, ?, ?))
class CreateFrontendUsers < ActiveRecord::Migration[5.1]
def change
create_table :frontend_users do |t|
t.string :email
t.timestamps
end
end
end
class CreateFrontendEvents < ActiveRecord::Migration[5.1]
def change
create_table :frontend_events do |t|
t.string :name
t.timestamps
end
end
end
class CreateFrontendEventsUsersJoinTable < ActiveRecord::Migration[5.1]
def change
create_table :frontend_events_users, id: false do |t|
t.integer :event_id
t.integer :user_id
end
add_index :frontend_events_users, :event_id
add_index :frontend_events_users, :user_id
end
end
class Frontend::Event < ApplicationRecord
has_and_belongs_to_many :users
end
class Frontend::User < ApplicationRecord
has_and_belongs_to_many :events
end
user = Frontend::User.create(email: "asdf")
event = user.events.create(name: "event1")
user.events # => #<ActiveRecord::Associations::CollectionProxy [#<Frontend::Event id: 1, name: "event1", created_at: "...", updated_at: "...">]>
event.users # => #<ActiveRecord::Associations::CollectionProxy [#<Frontend::User id: 1, email: "asdf", created_at: "...", updated_at: "...">]>
class CreateFrontendEventInvitations < ActiveRecord::Migration[5.1]
def change
create_table :frontend_event_invitations do |t|
t.integer :event_id
t.integer :user_id
t.timestamps
end
add_index :frontend_event_invitations, :event_id
add_index :frontend_event_invitations, :user_id
end
end
class Frontend::Event < ApplicationRecord
has_many :event_invitations
has_many :users, through: :event_invitations
end
class Frontend::User < ApplicationRecord
has_many :event_invitations
has_many :events, through: :event_invitations
end
class Frontend::EventInvitation < ApplicationRecord
belongs_to :user
belongs_to :event
end