扶手:调用从另一个模型中的一个模型。这是为什么不可能? [英] Rails: Calling one Model from another Model. Why is this not possible?
问题描述
我有以下型号...
教室<的ActiveRecord :: Base的
belongs_to的:酒店
belongs_to的:布局
的has_many:访问
验证:数字,presence:真
验证:速度,presence:真
验证:吸烟,presence:真
高清self.rooms_with_smoking(吸烟)
self.where('烟=?',吸烟)
结束
高清self.occupied_rooms(FROM_DATE,TO_DATE)#24-26
self.joins(:访问)('?日期和GT; =和日期< =',FROM_DATE,TO_DATE)。凡.uniq
结束
高清self.vacant_rooms(FROM_DATE,TO_DATE)
self.where.not(ID:Room.occupied_rooms(FROM_DATE,TO_DATE))
结束
高清self.blahblah(occupancy_count,吸烟,FROM_DATE,TO_DATE)
布局= Layout.layouts_with_occupancy_count_gt(occupancy_count)
Room.rooms_with_smoking(吸烟).vacant_rooms(FROM_DATE,TO_DATE).joins(布局)
结束
结束
该blahblah方法...
当我试图从IRB运行此我得到一个错误......未知类:布局
1)是否有房,知道关于布局。我不能用这样的另一种模式?我怎样才能做到这一点。
2)理论问题:是它最好有一个大的方法定义来获得一些信息...或者是它更好地有很多的小条块分割的方法,并利用它们都在同一个方法来获得较大的一块数据。例如...
一个大法:
高清self.find_rooms_with(occupancy_count,吸烟,FROM_DATE,TO_DATE)
Room.vacant_rooms(FROM_DATE,TO_DATE).joins(:布局)。凡('?occupancy_count> =',occupancy_count)。凡('?吸烟=',吸烟)
结束
绝不是大的,但一切都重新codeD。看起来它不将按照干长。
很多小方法:
高清self.blahblah(occupancy_count,吸烟,FROM_DATE,TO_DATE)
布局= Layout.layouts_with_occupancy_count_gt(occupancy_count)
Room.rooms_with_smoking(吸烟).vacant_rooms(FROM_DATE,TO_DATE).joins(布局)
结束
二是容易code。它采用3内置的方法定义。但是,是否有任何形式的表现打了第一个,或者是不太理想的?
修改:回答前两个意见如下...
当我刚类型的布局,或AP布局,我得到了我的整个布局模式。
类布局和LT;的ActiveRecord :: Base的{
:ID => :整数,
:说明=> :串,
:occupancy_count => :整数,
:created_at => :日期时间,
:的updated_at => :日期时间
}
=>零
和由IRB,是的,我的意思是轨道控制台。这里完整布局模式...
类布局和LT;的ActiveRecord :: Base的
的has_many:房间
验证:描述,presence:真
验证:occupancy_count,presence:真
高清self.layouts_with_occupancy_count(occupancy_count)
self.where('occupancy_count =?',occupancy_count)
结束
高清self.layouts_with_occupancy_count_gt(occupancy_count)
self.where('occupancy_count> =?',occupancy_count)
结束
结束
1),看来你不申报的布局类。
在 belongs_to的
类方法的预计中定义相应的ActiveRecord类。默认情况下它应该匹配方法名(:布局
对应布局
)
I have the following Model...
class Room < ActiveRecord::Base
belongs_to :hotel
belongs_to :layout
has_many :visits
validates :number, presence: true
validates :rate, presence: true
validates :smoking, presence: true
def self.rooms_with_smoking(smoking)
self.where('smoking = ?', smoking)
end
def self.occupied_rooms(from_date, to_date) #24-26
self.joins(:visits).where('date >= ? and date <= ?', from_date, to_date).uniq
end
def self.vacant_rooms(from_date, to_date)
self.where.not(id: Room.occupied_rooms(from_date, to_date))
end
def self.blahblah(occupancy_count, smoking, from_date, to_date)
layouts = Layout.layouts_with_occupancy_count_gt(occupancy_count)
Room.rooms_with_smoking(smoking).vacant_rooms(from_date, to_date).joins(layouts)
end
end
The blahblah method...
When I attempt to run this from irb I get an error... "unknown class: Layout"
1) Does Room not 'know' about Layout. Can I not use another Model like this? How can I do it.
2) Theory question: Is it better to have one large method definition to obtain some information... or is it better to have a lot of little compartmentalized methods, and use them all in one method to obtain a larger piece of data. For example...
One 'Large' Method:
def self.find_rooms_with(occupancy_count, smoking, from_date, to_date)
Room.vacant_rooms(from_date, to_date).joins(:layout).where('occupancy_count >= ?', occupancy_count).where('smoking = ?', smoking)
end
By no means large, but everything is re-coded. Doesn't seem like it would follow DRY for long.
Many Little Methods:
def self.blahblah(occupancy_count, smoking, from_date, to_date)
layouts = Layout.layouts_with_occupancy_count_gt(occupancy_count)
Room.rooms_with_smoking(smoking).vacant_rooms(from_date, to_date).joins(layouts)
end
The second one is easier to code. It utilizes 3 built in method definitions. However, does it have any sort of performance hit over the first one, or is it less desirable?
EDIT: Answering the first two comments below...
When I just type Layout, or ap Layout, I get my entire Layout model.
class Layout < ActiveRecord::Base {
:id => :integer,
:description => :string,
:occupancy_count => :integer,
:created_at => :datetime,
:updated_at => :datetime
}
=> nil
And by irb, yes I mean rails console. Full Layout Model here...
class Layout < ActiveRecord::Base
has_many :rooms
validates :description, presence: true
validates :occupancy_count, presence: true
def self.layouts_with_occupancy_count(occupancy_count)
self.where('occupancy_count = ?', occupancy_count)
end
def self.layouts_with_occupancy_count_gt(occupancy_count)
self.where('occupancy_count >= ?', occupancy_count)
end
end
1) It seems you do not declare the Layout class.
The belongs_to
class method expects a corresponding ActiveRecord class to be defined. By default it should match the method name (:layout
corresponds to Layout
).
这篇关于扶手:调用从另一个模型中的一个模型。这是为什么不可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!