我有两个模型,用户和帐户:
class User < ActiveRecord::Base
belongs_to :account, :conditions=>proc{" company_account = #{self.company_user} "}
end
class Account < ActiveRecord::Base
has_many :users
end
在Rails 3中,belongs_to和:conditions=>工作得很好,但在Rails 4中,我看到这个选项无效。
我尝试过belongs_to :account,
我正在使用Ruby on Rails 4.1,我想“合并”两个has_many模型关联的结果。也就是说,我有以下模型和关联:
class Article < ActiveRecord::Base
has_many :assigned_comments
has_many :unassigned_comments
end
class AssignedComment < ActiveRecord::Base
belongs_to :article
end
class UnassignedComment < ActiveRecord::Base
belongs_t
我有两个Rails模型,Client和CheckIn
class Client < ActiveRecord::Base
has_many :check_ins
end
class CheckIn < ActiveRecord::Base
belongs_to :client
end
通常,当我为一个:check_ins获取所有:client时,我调用:
Client.find(1).check_ins
我已经注意到,随着:check_ins数量的增加,我在浪费内存,所以我想返回10个最近的记录,以及一个最古老的记录。我还希望返回的项是一个#<ActiveR
只有当连接表有一个主键(id),并且只有一个主键(Id)时,我才能在ActiveAdmin中编辑一个连接表。
如果我试图设计一个只有两个字段的联接表(两个字段都有主键),当我尝试添加一个新记录: ActiveAdmin时,NilClass会返回一个NilClass错误。
模型:
class IngredientProduct < ActiveRecord::Base
belongs_to :product
belongs_to :ingredient
end
class Ingredient < ActiveRecord::Base
end
class Product
给定两个模型Player和Team,如何选择两个玩家所属的所有球队?
示例Rails模型和数据库表设置:
Rails模型:
class Player < ActiveRecord::Base
has_and_belongs_to_many :teams, uniq: true
end
class Team < ActiveRecord::Base
has_and_belongs_to_many :players, uniq: true
end
SQL表
teams players players_teams
+---------
所以我有一个包含ActiveRecord模型的gem
# one of 30 or so models used by a suite of applications
class MyModel < ActiveRecord::Base
# some library code
end
# in Rails app, I want to add some behavior to this model using good old ruby class reopening
class MyModel < ActiveRecord::Base
scope :active, {
是否可以为Rails 3中的ActiveRecord模型指定不同的模式?以下内容用于Rails 2中:
class Company < ActiveRecord::Base
set_table_name "hr.company"
end
这在带有消息Table myapp.hr.company doesn't exist的Rails 3中失败。
以下内容适用于简单模型:
class Company < ActiveRecord::Base
establish_connection "hr"
set_table_name "
我想知道我是不是唯一一个有这个问题的人。为了精确定位,我创建了两个非常基本的模型。
# user.rb
class User < ActiveRecord::Base
has_one :role, :inverse_of => :user
accepts_nested_attributes_for :role
end
# role.rb
class Role < ActiveRecord::Base
belongs_to :user, :inverse_of => :role
accepts_nested_attributes_for :user
en
我是Rails的新手,我创建了两个模型:用户模型和组模型。对于每一个用户,无论是男性还是女性,只有一组人,我不能把他们联系在一起
class Group < ActiveRecord::Base
has_many :user
end
class User < ActiveRecord::Base
belongs_to :group
end
我们知道,Rails5添加了ApplicationRecord作为我们的模型(ActiveRecord)继承的抽象类。
但基本上,我认为我们用ApplicationRecord做的每一个技术要求,我们也可以用ActiveRecord::Base做。例如:
module MyFeatures
def do_something
puts "Doing something"
end
end
class ApplicationRecord < ActiveRecord::Base
include MyFeatures
self.abstract_clas
我想知道如何将关联添加到我的模型中。假设我生成了两个模型
rails generate model User
rails generate model Car
现在,我想添加一个关联,这样模型就可以获得如下形式
class User < ActiveRecord::Base
has_many :cars
end
class Car < ActiveRecord::Base
belongs_to :user
end
问题是:如何通过迁移来应用这种修改,以便在数据库中获取cars_users表?我计划在我的代码中使用这个表。
我在Rails应用程序中使用了两个不同的数据库:MongoDB和MsSQL,分别使用Mongoid和activerecord-sqlserver-adapter适配器。一切都很好,但是在生成模型时存在一个问题。
问题是“如何以不同方式生成与MongoDB或MsSQL相关的模型?”
,例如,:我想生成与MongoID相关的People模型,使用MsSQL生成Animal模型。当我使用命令:rails g model Animal name:string生成时,它会生成与mongoid相关的模型。如何使用ActiveRecord生成模型动物,这意味着与MsSQL相关。
请帮帮我。谢谢
我必须学习如何在rails 4.1.0 (Ruby2.0)中创建模型。我需要创建两个模型:类别和职位(一对多)。好吧,我的舞步:
$ rails new test_work
$ cd test_work/
$ rails generate model Category \
> name:string
$ rails generate model Post \
> message:text
然后,我以这样的方式更改了我的模型文件:
class Category < ActiveRecord::Base
has_many :posts
end
class Post <
在我的rails应用程序ConfirmRequest.rb和Booking.rb中有两个型号。这些模型与这种信任是相互关联的:
class ConfirmRequest < ActiveRecord::Base
has_one :booking
end
class Booking < ActiveRecord::Base
belongs_to :confirm_request
end
因此,我想首先定义两个变量,其中包含已为其创建预订的confirm_requests,以及未为其创建预订的其他变量,如何在rails中定义这些变量?
我是rails的新手,我正在使用rails多态关联,我遇到了一个问题,我有两个模型EmployeeTable和ProductTable。两者在图片模型中都有一些图片
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
class EmployeeTable < ActiveRecord::Base
has_many :pictures, as: :imageable
end
class ProductTable < ActiveRecord::Base
我有一个包含两个字段的MySQL表:id,name。我必须执行如下查询:
SELECT id,
name,
5 AS tot
FROM examples
在rails中,我为这个表建立了一个模型:
def Example < ActiveRecord::Base
attr_accessor :tot
end
如果我尝试执行以下命令:
Example.select("examples.*, 5 as tot")
对于每一行,model的变量tot为null。
为什么?
如何赋值模型变量中不存在的列的值?
我有两个模型之间的关系
类别模型
class Category < ActiveRecord::Base
belongs_to :product
end
产品型号
class Product < ActiveRecord::Base
has_many :categories
end
我在products表中有category_id,但是当我在products表中创建新产品时,category_id为空。我是rails新手,有人能帮我吗?
我有两个模型
class User < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :user
end
我是否需要将列user_id添加到Product表中,或者rails是否默认添加了它?
我的Rails应用程序包含两个名为customer和order的模型
class Customer < ActiveRecord::Base
attr_accessible :name
end
class Order < ActiveRecord::Base
belongs_to :customer
# attr_accessible :title, :body
end
在控制台中,我创建了customer模型的实例:
c=Customer.new(:name=>"Noa")
现在我想创建引用"c“的instance to
我有两个Rails模型:
class User < ActiveRecord::Base
enum user_type: [:admin, :normal]
end
和
class Department < ActiveRecord::Base
end
我编写了以下Rspec测试:
require 'rails_helper'
RSpec.describe User, :type => :model do
it { should belong_to(:department).conditions(user_type: :admin)}
en
在我的Rails 3.0应用中,我有两个模型,Concepts和Categories。概念有一个映射到类别的category_id字段。由于各种原因,这些模型必须存储在两个不同的数据库中,这两个数据库都与我的主应用程序数据库不同:
class Concept < ActiveRecord::Base
establish_connection configurations[Rails.env]["db1"]
set_table_name "concepts"
end
class Category < ActiveRecord::Base