Ruby on Rails使用名称空间保存记录

时间:2018-03-14 01:11:32

标签: ruby-on-rails ruby

我使用Rails 5.1构建应用程序,我试图在命名空间中组织模型但是当我尝试保存实例时,我收到错误no such table: main.users也许这里有人可以显示我的方式!

app/models/frontend.rb

module Frontend
  def self.table_name_prefix
   'frontend_'
  end
end

我在这里使用设计用户迁移Frontend::User模型 app/models/frontend/user.rb

   class Frontend::User < ApplicationRecord
     # a user has many events (event creator)
     has_many :events
     # a user has and belongs to many event invitations
     has_and_belongs_to_many :event_invitations, class_name: 
     "Frontend::Event"

     # Include default devise modules. Others available are:
     # :timeoutable and :omniauthable, :confirmable
     devise :database_authenticatable, :registerable, :lockable,
      :recoverable, :rememberable, :trackable, :validatable
  end

Frontend::Event型号: app/models/frontend/event.rb

class Frontend::Event < ApplicationRecord
  belongs_to :user
end

Frontend::Event模型的迁移:

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

在rails控制台中:

我可以创建用户

u = Frontend::User.create(email: "user@user.com", password: "secret")

我可以使用has_many :events关系

的构建方法
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>

尝试保存记录e.save

时出现问题
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: 
main.users: INSERT INTO "frontend_events" ("user_id", 
"created_at","updated_at") VALUES (?, ?, ?))

1 个答案:

答案 0 :(得分:1)

我认为rails约定存在一些问题。据我所知,你应该以一种特殊的方式命名连接表。

您可以查看导轨指南,特别是creating join tables for has and belongs to many associations部分。

按照指南,我能够建造更少的建筑物,但没有设计。

在示例中,我有三次迁移......

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

在rails控制台中,我能够做到......

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: "...">]>

考虑通过关联使用has_many

如果您想要更多地控制联接表记录,通常的做法是通过关联使用“has many”。如果您还没有听说过它们,您还可以查看The has_many :through association部分中的指南。

您还可以重新创建更改CreateFrontendEventsUsersJoinTable迁移...

的示例
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

我almos总是喜欢使用这种关联而不是has_many_and_belongs_to关联。但你可以决定哪个更适合你=)

有时很难知道所有的rails约定,即使你已经使用Rails几年= S ......但Rails Guideshttp://api.rubyonrails.org/有很多帮助!

我希望它有帮助=)