如何限制多个关系中返回的行数?例如:
class User < ActiveRecord::Base
has_many :photos
end
Run Code Online (Sandbox Code Playgroud)
我希望能够做到:
User.includes(:photos => {:limit => 8}).all
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,但具有此功能的东西.我是否需要自己编写SQL?
提前致谢!
编辑:我不想限制关联,只是查询结果.所以用户可能有一千张照片,我只想要返回前三名.
我需要跟踪设置的关联的更改(添加和删除),如下所示:
has_many :listing_services
has_many :services, through: :listing_services
Run Code Online (Sandbox Code Playgroud)
对于普通属性,最简单的方法是签l.changes[attribute]入before_save或签l.previous_changes[attribute]入after_save.
问题是,为has_many属性执行此操作的最佳方法是什么?
RailsGuides说:
http://guides.rubyonrails.org/association_basics.html 一个has_many"关联表示模型的每个实例都有零个或多个另一个模型的实例."
"has_one关联还与另一个模型建立一对一的连接,但语义(和后果)有些不同.这种关联表明模型的每个实例都包含或拥有另一个模型的一个实例."
这是否意味着如果我想建立一个关联,模型的每个实例都有零或另一个模型的一个实例,最好的方法是使用has_many而不是has_one?如果我使用has_one会遇到什么问题?
谢谢.
对于以下情况,我使用嵌套表单遇到has_many/has_many关系问题.
我有文章,这可以有很多单位(Kg,袋等).通过我的表格,我需要记录这些数据.
所以我决定使用带有关系has_many的嵌套表单,但是我有一个错误: ArgumentError (No association found for name 'article_unit_of_measurements'. Has it been defined yet?)
这是我的Classes代码:
class Article < ActiveRecord::Base
has_many :unit_of_measurements, :through => :article_unit_of_measurements
accepts_nested_attributes_for :article_unit_of_measurements
end
class ArticleUnitOfMeasurement < ActiveRecord::Base
belongs_to :article
belongs_to :unit_of_measurement
accepts_nested_attributes_for :unit_of_measurement
end
class UnitOfMeasurement < ActiveRecord::Base
has_many :articles, :through => :article_unit_of_measurements
end
Run Code Online (Sandbox Code Playgroud)
在我的表单中使用jquery插件select2.js将我的选择单元显示为标签.
= simple_form_for([:shop, @article], html: {class: 'form-horizontal' }) do |f|
.col-md-10
= f.input :name, placeholder: "Name", :input_html => {:class => 'form-control'}, label: false
%label.col-md-2.control-label{for: "unit_of_measurement_id"} Unidad de medida
.col-md-10
%select#unit-select-measure.select2{:name =>"article[article_unit_of_measurements_attributes][#{Time.now.to_s}][unit_of_measurement_id]", …Run Code Online (Sandbox Code Playgroud) select has-many nested-forms nested-attributes ruby-on-rails-4
我有以下内容:
class Project < ActiveRecord::Base
has_many :project_people
has_many :people, :through => :project_people
end
class Person < ActiveRecord::Base
has_many :project_people
has_many :projects, :through => :project_people
end
class ProjectPerson < ActiveRecord::Base
belongs_to :project
belongs_to :person
scope :lead, where(:is_lead => true)
scope :member, where(:is_lead => false)
end
Run Code Online (Sandbox Code Playgroud)
将"引导"ProjectPerson添加到新项目时,它似乎构建正确,但在调用"@ project.project_people"时,该数组为空:
@project = Project.new
=> #<Project id: nil, name: nil>
@project.project_people.lead.build
=> #<ProjectPerson id: nil, project_id: nil, person_id: nil, is_lead: true>
@project.project_people
=> []
Run Code Online (Sandbox Code Playgroud)
当我在没有范围的情况下尝试此操作时,ProjectPerson将显示在数组中:
@project.project_people.build
=> #<ProjectPerson id: nil, project_id: nil, person_id: nil, is_lead: …Run Code Online (Sandbox Code Playgroud) 用户has_many解决方案
如何为拥有最多解决方案的用户订购用户?
我正在努力寻找十大用户,但我不确定如何做到最干净或最有效的方法呢?
有没有人有一个计算成本太高的例子?
两种型号:
class Task < ActiveRecord::Base
has_many :subtasks
end
class Subtask < ActiveRecord::Base
belongs_to :task
end
Run Code Online (Sandbox Code Playgroud)
子任务具有布尔属性,如果子任务完成,则该属性设置为true.
如何检查任务是否至少有一个完成的子任务?
我知道如何工作has_many :posts, dependent: :destroy.如果帖子User或has_many帖子被销毁,所有所属帖子也会被销毁.
但Post模型会发生什么belongs_to :user, dependent: :destroy?我在Rails指南中找到了这个选项,但我找不到如何使用它.
我有两个型号:(专辑和产品)
1)内部模型
在album.rb里面:
class Album < ActiveRecord::Base
attr_accessible :name
has_many :products
end
Run Code Online (Sandbox Code Playgroud)
内部product.rb:
class Product < ActiveRecord::Base
attr_accessible :img, :name, :price, :quantity
belongs_to :album
end
Run Code Online (Sandbox Code Playgroud)
2)使用" rails console ",如何设置关联(所以我可以使用"<%= Product.first.album.name%>")?
例如
a = Album.create( :name => "My Album" )
p = Product.create( :name => "Shampoo X" )
# what's next? how can i set the album and the product together?
Run Code Online (Sandbox Code Playgroud) 我有问题表,存储了这么多问题.与question_topics相关的问题因此创建与问题有很多关系.现在它看起来像这样:
$this->Question->bindModel(
array(
'hasMany' => array(
'QuestionTopic'=>array(
'className' => 'QuestionTopic',
'foreignKey' => 'question_id',
'dependent' => true,
'conditions' => array('QuestionTopic.areas_id' => 165),
'type' => 'left'
)
)
)
);
print_r($this->Question->find('all'));
die;
Run Code Online (Sandbox Code Playgroud)
当我看到结果时,它看起来像这样
Array
(
[0] => Array
(
[Question] => Array
(
[id] => 89
[user_id] => 1
[question_group_id] => 0
[question] => jQuery function here
[target_id] => 1
[type] => 1
[order] => 1
[description] => additional info here
[privacy_friend_id] =>
[channel_id] => 1
[status] => 0
[location] => Chandigarh, …Run Code Online (Sandbox Code Playgroud) has-many ×10
activerecord ×4
belongs-to ×3
associations ×2
ruby ×2
build ×1
cakephp ×1
has-one ×1
limit ×1
nested-forms ×1
scope ×1
select ×1