假设我有两个表,一个是包含个人信息的学生的主列表,另一个是班级的学生注册列表。这两个表共享一个公共列,该列是唯一标识学生的字符串,但它不是主键。
假设我想在一个页面上显示所有的注册信息,以及学生的一些个人数据(比如家乡)。
我理解这将是一种has_many关系。主列表记录有许多注册。注册属于某个学生。
class Student < ActiveRecord::Base
has_many :enrollments
end
class Enrollment < ActiveRecord::Base
belongs_to :student
end
这是两者之间的正确关系
假设数据库中的三个表之间存在以下关联:
//working with three tables a client 'has one' business
//and a business has many business hours.
下面将给我们一个Activerecord对象的数组
$this->client->business->businesshours
我们必须从数组中提取一个对象来获得它的列值:
$this->client->business->businesshours[0]->start_time
由于我
我的应用程序中有以下模型:
class Company < ActiveRecord::Base
has_many :gallery_cards, dependent: :destroy
has_many :photos, through: :gallery_cards
has_many :direct_photos, class_name: 'Photo'
end
class Photo < ActiveRecord::Base
belongs_to :gallery_card
belongs_to :company
end
class G
我有三个表--业务、用户和订阅。目标是能够获得与用户关联的业务名称。结果应该是用户已在订阅中注册的业务名称数组。
表表清单
Schema | Name | Type |
--------+-------------------+-------
public | businesses | table
public | subscriptions | table
public | users | table
业务表
Column | Type
我有Order和Orderdetails
Orderdetails belongs_to Order
Order has_many Orderdetails
我正在尝试将以下查询转换为ActiveRecord count函数
select Count(*)
from orderdetails A, orders B
where A.prodid='6' and A.orderid= B.id and B.custid='11'
我试过:
@count = Orderdetail.count(:conditions => "prodid = 6
我有三个模型,Application,它属于一个Board,它属于一个User。
class User < ActiveRecord::Base
has_many :boards
end
class Board < ActiveRecord::Base
belongs_to :user
has_many :applications
end
class Application < ActiveRecord::Base
belongs_to :board
end
我总是只想显示当前用户的主板或应用程序。我怎么能说“为当前用户显示当前单板的每一个申请”?主要是如
在我的Rails应用程序中,我有一个以下类型的多级层次结构:
class Vehicle < ActiveRecord::Base end
class RoadVehicle < Vehicle end
class Car < RoadVehicle end
class Buss < RoadVehicle end
然后我有一个引用中间层的类,如下所示:
class Garage < ActiveRecord::Base
has_many :road_vehicles
end
在这个简化的示例中,我为vehicles表提供了一个type列,以启用单表继承。此外
如果我设置了以下模型,这是在Authorship表中添加特定于该关系而不是其他关系的属性:foo的合适方式吗?
class Author < ActiveRecord::Base
has_many :authorships
has_many :books, :through => :authorships
end
class Book < ActiveRecord::Base
has_many :authorships
has_many :authors, :through => :authorships
end
class Authorship &
我有两个具有has_many关联的模型:
class Log < ActiveRecord::Base
has_many :log_details
end
和
class LogDetail < ActiveRecord::Base
belongs_to :log
end
Log表有一个action_type字符串列。LogDetail表有2列:key和value,都是字符串,还有一个对带有log_id的Log表的引用。
我想在Log模型上编写3个作用域,以查询一些细节,将Log表与LogModel连接两次。这是一个样本:
has_many :payment_gateway
假设我有一个类型模型,它可以与Book或Movie的实例相关联。就像,
class Genre < ActiveRecord::Base
belongs_to :genreable, polymorphic: true
end
class Book < ActiveRecord::Base
has_many :genres, as: :genreable
end
class Movie < ActiveRecord::Base
has_many :genres, as: :genreable
end
现在,假设我想将我的体裁数据规范化,所以每种类型在分类表中只
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
end
class User < ActiveRecord::Base
has_many :posts
has_many :comments
end
我试图拉出帖子数据,同时急于加载用户和评论数据,但限制是不加载已被阻止的评论(注释表中的TINYINT字段)。下面
我的Rails应用程序开始需要复杂的查询。我应该开始使用原始SQL查询吗?Rails社区的趋势是什么?
更新:
我现在没有书面质询,我想在开始前提出这个问题。但以下是我想要做的一个例子:
我有分类的书。我想说-
Give me all books that were:
-created_at (added to store) between date1 and date2
-updated_at before date3
-joined with books that exist in shopping carts right now
我还没有编写查询,但我认为rails版本会是这样的:
b
我有这样的课:
class Game < ActiveRecord::Base
has_many :offers
#table has an integer-column 'season'
end
class Broker < ActiveRecord::Base
has_many :offers
end
class Offer < ActiveRecord::Base
belongs_to :game
belongs_to :broker
end
举个例子,我想从一个经纪人那里选择所有的报价,比如2009年。我试过
在我的网站上(用sinatra写的),我正试图建立一个数据库。我有两个表,这里称为Table1和Table2。
models.rb
class Table1 < ActiveRecord::Base
Table1.where(bool:true) has_one :table2 # PSUDO-CODE
# So that every record where bool:true has the relationship
# but every record where bool:false or bool:nil doesn't
end
class Ta
我有一个叫User的模态
class User < ActiveRecord::Base
has_many: :posts
end
还有另一个叫Post的模式
class Post < ActiveRecord::Base
belongs_to :user
end
如果我有一个作用域posts对象(比如posts = Post.where(id: [1,2,3,4])
如何使用活动记录方法查询所有帖子的uniq用户?
我知道它可以在两个查询中完成,有方法在一个查询中完成吗?
这个是可能的吗?
class CarActivity < ActiveRecord::Base
belongs_to :inspection
end
class Inspection < ActiveRecord::Base
belongs_to :car_activity # instead of using a has_one here
end
目标是:
使检查DB记录更容易:在表列(使用hirb )查看检查是否有关联的car_activity,而无需遍历检查和检查对象的car_activity引用。
使查询更容易:在没有关联car_activity的情况下获得
我正在努力让一个关联在Rails中正常工作。我只是在学习这门语言,所以任何帮助都是非常感谢的。
我有:
class Feedback < ActiveRecord::Base
belongs_to :section
end
class Section < ActiveRecord::Base
has_many :questions
has_many :feedbacks
end
我的'feedbacks‘表有一个'section_id’列。
然后在我的/show.html.erb in sections中,我有:
<% @secti