我正在通过Mongoid Ruby gem with Rails (Rails3beta3)学习MongoDB,并且我正在尝试想出一种方法,基于来自另一个模型的字段在模型上创建动态属性,我认为无模式的数据库是一个很好的选择。
举个例子,我有这样的模型:
class Account
include Mongoid::Document
field :name, :type => String
field :token, :type => String
field :info_needed, :type => Array
embeds_many :membe
我在Rails 3应用程序中有一个嵌套的资源模型。这是一个标准的博客应用程序,有帖子和评论。我刚刚开始使用jQuery等来使我的应用程序更加动态,我现在正努力用link_to助手删除嵌套模型中的注释。
注释模型
class Comment < ActiveRecord::Base
belongs_to :post
end
Post模型
class Post < ActiveRecord::Base
has_many :comments, :dependent => :destroy
accepts_nested_attributes_for :comments
e
使用has_and_belongs_to_many丢弃 Gem的软删除关联的最佳方法是什么。有了这个关联,数据库中就有一个连接表,但是没有实际的Rails模型。
我将尝试通过一个例子来解释,假设有两个模型:
class Participant < ApplicationRecord
has_and_belongs_to_many :company_employees
end
class CompanyEmployee < ApplicationRecord
has_and_belongs_to_many :participants
end
这将在数据库中创建compa
就像rails中的动态查找器方法一样,有没有办法为相关的模型提供动态查找器方法?
请考虑以下模型
class User
attr_accessible :name, :phone_no
has_many :notes
end
class Note
belongs_to :user
attr_acccessible :note
end
如何从User对象调用note属性的动态查找器?
如何使模型中的主页面片段过期?
在我的HTML中
<% cache 'main-page' do %>
# html here
<% end %>
在我的邮政模型中
after_create :clear_cache
after_update :clear_cache
def clear_cache
ActionController::Base.new.expire_fragment('main-page')
end
这不能清除缓存。如果我创建或更新了一个帖子,缓存就不会清除。如果我在rails控制台中运行ActionContr
我在rails中创建了模型,使用
rails g model User uid:integer name:string
和
rails g model Post message:string user:references
上面的代码行生成了如下模型
class User < ActiveRecord::Base
attr_accessible :name, :uid
end
和
class Post < ActiveRecord::Base
belongs_to :user
attr_accessible :message
end
现在,我是否需要显式地在User模型
我想要创建名称空间模型,例如:
rails g model aaa
rails g model aaa/bbb
rails g model aaa/ccc
当我运行rails g model aaa/bbb时,Rails将自动生成:
module AAA
def self.table_name_prefix
'aaa_'
end
end
AAA也是一个模型,所以我需要将它放在模块中:
Module AAA
def self.table_name_prefix
'staff_'
end
class AAA < Activ
我正在尝试动态创建和删除rails模型。创建可以工作,但不能删除。
我尝试将它作为常量删除,但它仍然作为rails子类存在:
Object.send(:remove_const, :ModelToBeDeleted)
# Check it's gone from Object
Object.constants.include? :ModelToBeDeleted # => false
# But it is still in rails:
ActiveRecord::Base.subclasses
# returns
[ModelToBeDeleted(....)]
然后,
在Rails中,我创建了一个从LDAP数据库而不是从ActiveRecord检索用户的模型。现在,我正在尝试将我的ActiveRecord模型与基于LDAP的模型集成,因此我在我的模型中编写模拟一些常见ActiveRecord方法的方法。
我试图模拟的方法之一是通常由ActiveRecord上的has_many通过关系创建的方法。在ActiveRecord中,此关系将允许以下内容:
user = User.first
groups = user.groups # == Array of Groups
groups << Group.create(name: "Test