Rails 5 和 ActiveModelSerializers
Rails 5 and ActiveModelSerializers
我想在 Rails 5 中学习一些关于 ActiveModelSerializers 的知识。我正在关注这个 tutorial 和 运行 的一些错误:
我可能不应该乱用 Rails 5 beta 对吗?
rails -v
Rails 5.0.0.beta3
和Ruby:
ruby -v
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin15]
这是我的架构:
ActiveRecord::Schema.define(version: 20160303205439) do
create_table "rental_units", force: :cascade do |t|
t.string "address"
t.integer "rooms"
t.integer "bathrooms"
t.integer "price_cents"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
add_index "rental_units", ["user_id"], name: "index_rental_units_on_user_id"
create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email"
end
end
这是我的种子:
User.create(name: "1st user", email: "1stemail@gmail.com")
User.create(name: "2nd user", email: "2ndemail@gmail.com")
User.create(name: "3rd user", email: "3rdemail@gmail.com")
RentalUnit.create(address: "1st address", rooms: 2, bathrooms: 2, price_cents: 50000, user_id:1)
RentalUnit.create(address: "2nd address", rooms: 2, bathrooms: 2, price_cents: 50000, user_id:1)
RentalUnit.create(address: "3rd address", rooms: 2, bathrooms: 2, price_cents: 50000, user_id:1)
RentalUnit.create(address: "4th address", rooms: 2, bathrooms: 2, price_cents: 50000, user_id:2)
这是我的 RentalUnit 模型:
class RentalUnit < ApplicationRecord
end
这是我的 RentalUnitSerializer:
class RentalUnitSerializer < ActiveModel::Serializer
attributes :id, :address, :rooms, :bathrooms, :price, :price_per_room
belongs_to :user
end
当我尝试 运行 rails 服务器时,我得到:
rs
=> Booting Puma
=> Rails 5.0.0.beta3 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/Jwan/Dropbox/programming/rails/api_codeship/app/serializers/rental_unit_serializer.rb:3:in `<class:RentalUnitSerializer>': undefined method `belongs_to' for RentalUnitSerializer:Class (NoMethodError)
from /Users/Jwan/Dropbox/programming/rails/api_codeship/app/serializers/rental_unit_serializer.rb:1:in `<top (required)>'
...
知道发生了什么吗?
有人有任何关于 API 设计、Rails API 和 AMS 的好教程吗?
您的 RentalUnit
模型中缺少 has_one
或 has_many
关系(也许还有 User
模型)。根据需要,在 RentalUnit
模型中添加 belongs_to :user
,在 User
模型中添加 has_one
或 has_many
。
我正在使用 Rails 5beta2 和 ActiveModel Serializer,它工作正常......我猜 beta3 应该只会更好。
我想在 Rails 5 中学习一些关于 ActiveModelSerializers 的知识。我正在关注这个 tutorial 和 运行 的一些错误:
我可能不应该乱用 Rails 5 beta 对吗?
rails -v
Rails 5.0.0.beta3
和Ruby:
ruby -v
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin15]
这是我的架构:
ActiveRecord::Schema.define(version: 20160303205439) do
create_table "rental_units", force: :cascade do |t|
t.string "address"
t.integer "rooms"
t.integer "bathrooms"
t.integer "price_cents"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
add_index "rental_units", ["user_id"], name: "index_rental_units_on_user_id"
create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email"
end
end
这是我的种子:
User.create(name: "1st user", email: "1stemail@gmail.com")
User.create(name: "2nd user", email: "2ndemail@gmail.com")
User.create(name: "3rd user", email: "3rdemail@gmail.com")
RentalUnit.create(address: "1st address", rooms: 2, bathrooms: 2, price_cents: 50000, user_id:1)
RentalUnit.create(address: "2nd address", rooms: 2, bathrooms: 2, price_cents: 50000, user_id:1)
RentalUnit.create(address: "3rd address", rooms: 2, bathrooms: 2, price_cents: 50000, user_id:1)
RentalUnit.create(address: "4th address", rooms: 2, bathrooms: 2, price_cents: 50000, user_id:2)
这是我的 RentalUnit 模型:
class RentalUnit < ApplicationRecord
end
这是我的 RentalUnitSerializer:
class RentalUnitSerializer < ActiveModel::Serializer
attributes :id, :address, :rooms, :bathrooms, :price, :price_per_room
belongs_to :user
end
当我尝试 运行 rails 服务器时,我得到:
rs
=> Booting Puma
=> Rails 5.0.0.beta3 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/Jwan/Dropbox/programming/rails/api_codeship/app/serializers/rental_unit_serializer.rb:3:in `<class:RentalUnitSerializer>': undefined method `belongs_to' for RentalUnitSerializer:Class (NoMethodError)
from /Users/Jwan/Dropbox/programming/rails/api_codeship/app/serializers/rental_unit_serializer.rb:1:in `<top (required)>'
...
知道发生了什么吗?
有人有任何关于 API 设计、Rails API 和 AMS 的好教程吗?
您的 RentalUnit
模型中缺少 has_one
或 has_many
关系(也许还有 User
模型)。根据需要,在 RentalUnit
模型中添加 belongs_to :user
,在 User
模型中添加 has_one
或 has_many
。
我正在使用 Rails 5beta2 和 ActiveModel Serializer,它工作正常......我猜 beta3 应该只会更好。