有没有快速的方法为embeds_many-embedded_in关系制作表单?我有以下内容:
class Team
include Mongoid::Document
field :name, :type => String
embeds_many :players
end
class Player
include Mongoid::Document
embedded_in :team, :inverse_of => :players
field :name, :type => String
end
Run Code Online (Sandbox Code Playgroud)
我想为团队创建一个表格,为玩家提供嵌入式编辑.在那里看到https://github.com/bowsersenior/formtastic_with_mongoid_tutorial但"TODO".
Rails 3.2.1
应用名称:demo
数据库:mongoDB与mongoid
我在rails 3.2.1中设置了这个脚手架:localhost:3000/pages,
我有这些领域:标题,内容.
我已经为每个字段填充了3行数据.
问题是:删除应用程序(删除根文件夹)并使用相同的脚手架(页面)创建相同的应用程序名称(演示)后,这3行数据仍然显示,我不再需要了.
任何人都可以如何清理数据库?谢谢
好的,刚刚得到:
class User
include Mongoid::Document
# devise stuff...
has_one :card
end
class Card
include Mongoid::Document
belongs_to :user
end
irb(main):012:0> user = User.new(email: "test@test.com", password: "testtest",password_confirmation: "testtest")
=> #<User _id: 4f4e1b1494b3023d34000003, _type: nil, email: "test@test.com", encrypted_password: "$2a$10$yCdmjfgK94VrIec062ZXFuoTuApFFaFsDSOVEutoklcSGc4SQH2MG", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>
irb(main):013:0> user.build_card uuid: "test"
=> #<Card _id: 4f4e1b2094b3023d34000004, _type: nil, created_at: nil, updated_at: nil, uuid: "test", user_id: BSON::ObjectId('4f4e1b1494b3023d34000003')>
irb(main):014:0> user.new_record?
=> true
irb(main):015:0> user.card.new_record?
=> true
irb(main):016:0> user.save …Run Code Online (Sandbox Code Playgroud) (Ruby 1.9.3,MongoDB 2.0.4,Rails 3.2,Mongoid 2.4,Geocoder 1.1.1)
我有以下型号:
class Company
include Mongoid::Document
embeds_one :office
index [[ "office.coordinates", Mongo::GEO2D ]]
end
class Office
include Mongoid::Document
include Geocoder::Model::Mongoid
geocoded_by :address
field :city, :type => String
field :state, :type => String
field :coordinates, :type => Array
embedded_in :company
after_validation :geocode
def address
"#{city}, #{state}"
end
end
Run Code Online (Sandbox Code Playgroud)
我在控制台中这样做:
> c = Company.new
=> #<Company _id: 4f885aa56d20f03898000003, _type: nil>
> c.office = Office.new(:city => "San Francisco", :state => "CA")
=> #<Office _id: 4f885ab66d20f03898000004, _type: nil, city: …Run Code Online (Sandbox Code Playgroud) 说我有一个mongoid类
Class User
include Mongoid::Document
field :username, type: String
field :age, type: Integer
before_save :remove_whitespace
def remove_whitespace
self.username.strip!
self.age.strip!
end
end
Run Code Online (Sandbox Code Playgroud)
在方法中remove_whitespace; 有没有更好的方法迭代所有字段使用块和迭代器去除它们而不是单独键入每个字段(self.username.strip!)?我班上有大约15个领域,正在寻找一个优雅的解决方案.
我在rails应用程序中有这种非常奇怪的行为.所有查找查询都运行了两次.我已经通过在ap端和DB端记录查询来验证这一点.我只是在开发模式下测试过它.
我已经在方法中添加了日志,以查看方法是否被多次调用,但事实并非如此.
所有保存只运行一次,因此没有问题.由于数据库查询所有数据两次,因此只有页面加载的问题会越来越慢.
我甚至不知道我应该粘贴哪些代码部分,因为我不知道这可能来自何处.
我正在使用 mongoid (3.0.13), rails (3.1.3)
以下是整个源代码的github链接https://github.com/deiga/new-Roydon/tree/develop
我知道这有很多问题.我环顾四周,但我似乎没有犯错,我找到了答案.
我的锻炼资源的表单嵌套在用户之下,正在给出错误
undefined method `workouts_path' for #<#<Class:0x007f88939c9e60>:0x007f8891527b20>
Run Code Online (Sandbox Code Playgroud)
那种形式:
= form_for @workout do |f|
... = f.submit
Run Code Online (Sandbox Code Playgroud)
Workouts Controller新动作(产生错误的地方):
def new
@user = User.find(params[:user_id])
@workout = @user.workouts.new
end
Run Code Online (Sandbox Code Playgroud)
锻炼模型以其独特的名字命名.控制器是复数.这是我的路线文件:
resources :users do
resources :workouts
end
Run Code Online (Sandbox Code Playgroud)
相关的佣金路线输出:
user_workouts GET /users/:user_id/workouts(.:format) workouts#index
POST /users/:user_id/workouts(.:format) workouts#create
new_user_workout GET /users/:user_id/workouts/new(.:format) workouts#new
edit_user_workout GET /users/:user_id/workouts/:id/edit(.:format) workouts#edit
user_workout GET /users/:user_id/workouts/:id(.:format) workouts#show
PUT /users/:user_id/workouts/:id(.:format) workouts#update
DELETE /users/:user_id/workouts/:id(.:format) workouts#destroy
Run Code Online (Sandbox Code Playgroud)
(也许它应该转到user_workouts_path,但我不知道为什么它不会自动执行.
此外,我正在使用mongo/mongoid,但我不知道为什么这应该有所作为.我让我的控制器放了@workout,它实例化了一个我可以在服务器日志中看到的新对象:
ActionView::Template::Error (undefined method `workouts_path' for #<#<Class:0x007f8893d0a480>:0x007f8893d12cc0>):
1: = form_for @workout do |f|
2:
3: = f.label :name, "Name …Run Code Online (Sandbox Code Playgroud) 我的模特很少:
class Product
...
embeds_many :items
...
end
class Item
...
embedded_in :product
...
end
class Cart
...
embeds_many :line_items
...
end
class LineItem
...
embedded_in :cart
belongs_to :product
belongs_to :item
...
end
Run Code Online (Sandbox Code Playgroud)
line_item.item属于Product中嵌入的Item
为什么LineItem的项目总是为零?
ruby-on-rails mongodb mongoid ruby-on-rails-3 ruby-on-rails-4
如何跳过mongoid中关系的默认范围?
可转移的关注点在模型上实现了软删除,并添加了以下内容
field :d_at, type: DateTime
default_scope -> { where(d_at: nil) }
Run Code Online (Sandbox Code Playgroud)
如果一个品牌遭到破坏,我仍然希望在我加载一个引用该品牌的产品时可以使用它们这些是模型定义
class Product
include Mongoid::Document
field :title, type: String
belongs_to :brand, class_name: 'Brand'
end
class Brand
include Mongoid::Document
include Concerns::Trashable
field :title, type: String
end
Run Code Online (Sandbox Code Playgroud)
例:
product = Product.find([id])
puts product.brand.inspect #This brand is soft-deleted and not fetched because of the default scope
Run Code Online (Sandbox Code Playgroud)
这样可行,但它会解决更多问题
class Product
include Mongoid::Document
field :title, type: String
belongs_to :brand, class_name: 'Brand'
#unscope relation to brand
def brand
Brand.unscoped.find(self.brand_id)
end
end
Run Code Online (Sandbox Code Playgroud) 如果我有几个ID,请说 [1,2,3]
是否可以在Mongoid中一次查询所有内容?如:
User.where({ id: [1,2,3]}) 或类似的东西?
mongoid ×10
mongodb ×4
ruby ×3
mongoid4 ×2
activemodel ×1
formtastic ×1
mongoid3 ×1
routes ×1
sanitization ×1
validation ×1