我是Rails的新手.
我有两个型号分类和产品如下: -
class Category < ActiveRecord::Base
attr_accessible :type
has_many :products
end
class Product < ActiveRecord::Base
attr_accessible :category_id, :color, :price, :title
belongs_to :category
end
Run Code Online (Sandbox Code Playgroud)
我的schema.rb如下: -
ActiveRecord::Schema.define(:version => 20130725220046) do
create_table "categories", :force => true do |t|
t.string "type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "products", :force => true do |t|
t.integer "category_id"
t.decimal "price", :precision => 10, :scale => 0
t.string "title"
t.string "color"
t.datetime "created_at", :null …
Run Code Online (Sandbox Code Playgroud) 我有一个Mongoid 的category
领域type
Array
.
防爆.类别:["val1","val2","val3"]
现在我想用`category:["val1","val2"]查询这个模型,这样它就会返回我的合并
Model.where(类别:"val1")和Model.where(类别:"val2")
我可以为数组的每个元素单独执行,但这会很慢,因为对于每个单独的元素,它将搜索所有文档.
我也尝试过Model.all_of({category: "val1"},{category: "val2"}).all
但是没有用.
我该怎么做?
我差不多完成了Ruby,但现在想尝试一下Ruboto,ruby on android.谷歌没有给我足够的(几乎没有结果).所以任何人都可以分享关于Ruboto的一些教程.
首先,我是rails的新手.我在rails网站上工作.它有三个控制器,即 application_controller,static_pages_controllers和users_controller.除了static_pages_controllers 之外,它们在app/assets/stylesheets /(application.css和users.css.scss)中都有各自的css(scss)文件, 并且还有一个custom.css.scss用于整体布局或公共元素.我使用了控制器特定的这里提到的样式表
我的问题是:
1)custom.css中的css规则是否适用于所有控制器视图,除了那些我在单独的控制器css中明确定义的视图?
2)如果是,那么我在custom.css.scss和users.css.scss中都定义了一个规则
custom.css.scss - body {background-color:color1;}
users.css.scss - body {background-color:color2;}
但是static_pages_controllers和users_controllers中的视图仍然显示背景color2.我哪里错了?只有users_controller中的视图必须显示color2,而static_pages_controller必须显示color1.
我想扫描未知数量的线,直到扫描所有线.我怎么用红宝石做到这一点?
例如:
put returns between paragraphs
for linebreak add 2 spaces at end
_italic_ or **bold**
Run Code Online (Sandbox Code Playgroud)
输入不是来自'文件',而是来自STDIN.
首先,任何人都可以解释独特的索引如何在数据库中工作?
假设我有一个带有a的用户模型,name column
我在其unique index
上添加了一个但是在模型中(user.rb)我presence
在该name
字段上只有一个验证器.
所以现在当我尝试创建两个具有相同名称的用户时,我得到了PGError
重复键值违反唯一约束"index_users_on_name"
所以它看起来像unique index
工作一样uniqueness validator
(?)
如果是这样那么外键呢?
可以说我有Post
与之belongs_to :user
关联的模型User has_many :posts
.和外键user_id
的posts table
具有唯一索引.然后多个帖子不能相同user_id
.
有人可以解释一下unique index
有效吗?
我使用Ruby 2.0.0在Rails 4上.
我想获得values
单个记录的多列(希望是一个数组)。我有column names
一个字符串数组。
就像是:
Model.first.select([:id,:name,:col1,:col2]).values
#=> [1,mrudul,col1_val,col2_val]
# with column_names as array
# Model.first.select(columns_names.map(&:to_sym)).values
Run Code Online (Sandbox Code Playgroud)
问题是我将列名作为字符串数组。我怎样才能做到这一点?
我正在处理电话号码字段,所以我需要它存在
存在:真实
别名
存在:{消息:“号码是必需的”}
而且我希望它的长度为 10。所以我做了
存在:{消息:“需要数字”},长度:{是:10}
现在我希望如果输入的数字不是 10 位数字,则应显示一条消息。我试过这个:
存在:{message: "Number is requred"} , length: {is: 10, message: "Number must be 10 digit long."}
但它不起作用。我应该如何实施?
我有一个订单控制器和*订单模型*像这样; -
class OrderController < ApplicationController
def new
@cart=current_cart
if @cart.items.empty?
flash[:error]="Your cart is empty"
redirect_to :back
return
end
@order=Order.new
end
def create
@order=Order.new
end
end
Run Code Online (Sandbox Code Playgroud)
我的routes.rb有
get "order/new"
resources :orders
Run Code Online (Sandbox Code Playgroud)
和new.html.erb中的表单
<%= simple_form_for(@order, html: {class: 'form-horizontal control-group '}) do |f| %>
<%= f.button :submit, "Place Order", class: "btn btn-large btn-primary" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我的表格中还有其他字段.
但是当我提交表单时,它会抛出错误
未初始化的常量OrdersController
怎么了?
ruby ×4
activerecord ×1
css ×1
eof ×1
indexing ×1
inheritance ×1
mongoid ×1
postgresql ×1
ruboto ×1
sass ×1
validation ×1