模型场景:
A node can belong to a parent node and can have child nodes.
Run Code Online (Sandbox Code Playgroud)
车型/ node.rb
class Node < ActiveRecord::Base
has_many :children, class_name: "Node", foreign_key: "parent_id"
belongs_to :parent, class_name: "Node"
end
Run Code Online (Sandbox Code Playgroud)
分贝/迁移/ 20131031144907_create_nodes.rb
class CreateNodes < ActiveRecord::Migration
def change
create_table :nodes do |t|
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后我想做迁移添加关系:
class AddNodesToNodes < ActiveRecord::Migration
def change
add_column :nodes, :parent_id, :integer
# how do i add childen?
end
end
Run Code Online (Sandbox Code Playgroud)
如何在迁移中添加has_many关系?
这个问题是Rails中HABTM协会的一个分支:收集和计算模型儿童的类别.
class Category < ActiveRecord::Base
has_and_belongs_to_many :books
validates_uniqueness_of :name
end
class Book < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Store < ActiveRecord::Base
has_many :books
has_many :categories, through: :books
end
Run Code Online (Sandbox Code Playgroud)
给定商店,列出每个类别的书籍数量.
Store.first.books_per_category
Run Code Online (Sandbox Code Playgroud)
期望的输出:
[ { name: 'mystery', count: 5 }, { name: 'fantasy', count: 6 } ]
Run Code Online (Sandbox Code Playgroud)
然而,每家商店可能拥有大量的书籍和类别.
我正在尝试创建一个单一的高性能查询,该查询只获取名称列和与商店关联的每个不同类别的书籍计数,而不将书籍加载到内存中.
class Store < ActiveRecord::Base
# Will load each book into memory
def books_per_category
categories.eager_load(:books).map do |c|
{
name: c.name,
count: c.books.size # Using size instead …Run Code Online (Sandbox Code Playgroud) 我是编程和轨道的新手,有一些我不完全理解的东西.我正在创建一个应用程序
product has_many categories
category has_many products
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,我需要创建一个具有product_id&a 的连接表products_categories category_id.首先,我还需要这个表的模型吗?如果是的话,我想它会是这样的:
class CategoryProduct < ActiveRecord::Base
belongs_to :category
belongs_to :product
end
Run Code Online (Sandbox Code Playgroud)
和product.rb中的其他模型:
class Product < ActiveRecord::Base
has_many :category_products
has_many :categories, through: :category_product
has_attached_file :picture,
styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :picture,
content_type: /\Aimage\/.*\z/
validates :price, presence: { message: "Merci d'indiquer le prix du produit" }
validates :name, presence: { message: "Merci d'indiquer le nom du produit" }
validates :weight, presence: { message: "Merci d'indiquer le poids du …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的 ngrok 隧道主机名列入白名单config.hosts,但每次启动 Ngrok 时它都会发生变化。有没有办法获取我的 Ngrok 隧道的公共 url,这样我就不必使用hosts.clear?
我有一个动作,让客户预览系统电子邮件,我想发送text/plain明文版电子邮件的标题.
我试图遵循Symfony文档:Symfony部分中的请求和响应.然而,text/html无论我做什么,我的控制器都会发送内容类型.
这是我的行动:
function showAction($action = null, $format = null){
$locale = $this->get('session')->getLocale();
$format = $this->getRequest()->get("format");
$format = isset($format) ? $format : 'html';
if ($format === 'text'){
$response = new Response();
$response->headers->set('Content-Type', 'text/plain');
$response->sendHeaders();
}
$view = sprintf('MyBundle:Email:%s.%s.%s.twig',
$action,$locale,$format);
return $this->render($view, array());
}
Run Code Online (Sandbox Code Playgroud)
那么如何发送文本普通标题以及我哪里出错?
案件:
我的工作站表格包含一个段塞字段,如果输入一个值,它应该用作段塞.
编辑:一些澄清:
我想要的就像slupress如何在wordpress中工作:
我的问题:
无法弄清楚如何让友情ID使用用户提供的slug.
class Station < ActiveRecord::Base
extend FriendlyId
belongs_to :user
has_many :measures
validates_uniqueness_of :hw_id
validates_presence_of :hw_id
class_attribute :zone_class
self.zone_class ||= Timezone::Zone
friendly_id :name, :use => [:slugged, :history]
before_save :set_timezone!
....
def should_generate_new_friendly_id?
name_changed? or slug_changed?
end
end
Run Code Online (Sandbox Code Playgroud)
编辑:
<%= form_for(@station) do |f| %>
<%=
f.div_field_with_label(:name) do |key|
f.text_field(key)
end
%>
<%=
f.div_field_with_label(:slug) do |key|
f.text_field(key)
end
%>
<%=
f.div_field_with_label(:hw_id, 'Hardware ID') do |key|
f.text_field(key)
end …Run Code Online (Sandbox Code Playgroud) 我正在尝试对所有站点进行查询,并加入Measures
但我只想要最新的措施(由created_at DESC排序),因为一个站有数千个措施.
我试过了
Station.joins(:measures).limit(1)
Run Code Online (Sandbox Code Playgroud)
但这只是限制了电台.
附加信息:
车站有很多措施
措施属于Station
我已经阅读了Active Records文档,并且只有关于在关联中使用where条件的信息.
该应用程序仅针对Postgres,SQL已被接受.
编辑:添加除schema.rb之外:
create_table "measures", force: true do |t|
t.integer "station_id"
t.float "speed"
t.float "direction"
t.float "max_wind_speed"
t.float "min_wind_speed"
t.float "temperature"
t.datetime "created_at"
t.datetime "updated_at"
t.float "speed_calibration"
end
add_index "observations", ["created_at"], name: "index_observations_on_created_at", using: :btree
add_index "observations", ["station_id"], name: "index_observations_on_station_id", using: :btree
create_table "stations", force: true do |t|
t.string "name"
t.string "hw_id"
t.float "latitude"
t.float "longitude"
t.float "balance"
t.boolean "offline"
t.string …Run Code Online (Sandbox Code Playgroud) 我在OS X 10.10.3上安装:自制软件,命令行工具,然后使用rbenv安装ruby:
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
Rails 4.2.2
Run Code Online (Sandbox Code Playgroud)
当我创建一个新的应用程序(使用默认的sqlite数据库)并尝试运行它时,它说:
=> Booting WEBrick
=> Rails 4.2.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-06-18 23:11:30] INFO WEBrick 1.3.1
[2015-06-18 23:11:30] INFO ruby 2.2.2 (2015-04-13) [x86_64-darwin14]
[2015-06-18 23:11:30] INFO WEBrick::HTTPServer#start: pid=41860 port=3000
Run Code Online (Sandbox Code Playgroud)
但在浏览器中我看到:
500 Internal Server Error
If you are the administrator of this website, then please read this web application's log file and/or the web …Run Code Online (Sandbox Code Playgroud) 我正在我的本地机器(OS-X 10.10)上进行测试,该机器使用不区分大小写的文件系统(HFS + [CI]) - 当我重置到头部时:
$ git reset head --hard
$ git reset HEAD --hard
Run Code Online (Sandbox Code Playgroud)
和
$ git checkout 4f2c
$ git checkout 4F2C
Run Code Online (Sandbox Code Playgroud)
有完全相同的结果.据我所知,GIT将refs存储在内部,./git/refs但底层文件系统的区分大小写会影响结果吗?
我会在区分大小写的文件系统上获得相同的结果吗?
使用wispergem 有什么好处rails-observers吗?
他们乍一看看起来非常相似,但是社区似乎更加支持Wisper(基于GH明星,提交和发布).它们之间是否存在显着差异?
ruby ×3
postgresql ×2
activerecord ×1
forms ×1
friendly-id ×1
git ×1
jointable ×1
model ×1
ngrok ×1
osx-yosemite ×1
performance ×1
php ×1
rbenv ×1
rubygems ×1
symfony ×1
wisper ×1