我有以下型号:
User (id, name, network_id)
Network(id, title)
Run Code Online (Sandbox Code Playgroud)
我需要添加什么样的Rails模型关联才能执行以下操作:
@user.network.title
@network.users
Run Code Online (Sandbox Code Playgroud)
谢谢
我用 postgresql db 创建了一个新的 rails 5 应用程序并修改了 database.yml 文件。我成功地创建了开发和测试数据库,但是在运行迁移时只更新开发数据库,测试数据库保持不变。
这是我使用的命令列表:
rails db:create # Created both development and test
rails db:migrate # Migrated only to development
rails db:migrate RAILS_ENV=test # Does nothing (no error output)
rake db:migrate RAILS_ENV=test # Same result as above
Run Code Online (Sandbox Code Playgroud)
我的 database.yml 文件:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
username: myapp_admin
password:
host: localhost
port: 5432
test:
<<: *default
database: myapp_test
username: myapp_admin
password:
host: localhost …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个函数来完成以下哈希中的小时序列.
{
name: "cardio",
data: [["06:00", 999], ["09:00", 154], ["10:00", 1059], ["11:00", 90]]
}
Run Code Online (Sandbox Code Playgroud)
它应该在字段数据中创建所有缺少的值
["07:00", 0], ["08:00", 0], ["12:00", 0], ["13:00", 0] ... ["23:00", 0]
Run Code Online (Sandbox Code Playgroud)
预期结果:
{
name: "cardio",
data: [["06:00", 999], ["07:00", 0], ["08:00", 0], ["09:00", 154], ["10:00", 1059], ["11:00", 90]], ["12:00", 0], ["13:00", 0] ... ["23:00", 0]
}
Run Code Online (Sandbox Code Playgroud)
有可能吗?就像是:
data.each do |row|
(6..23).each do |hour|
.....
end
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试在heroku中部署我的应用程序,但是由于此错误,我无法执行任何操作:
Rails ::机密:: MissingKeyError:缺少用于解密机密的加密密钥。向您的团队询问您的主密钥,并将其放入ENV [“ RAILS_MASTER_KEY”]
有人能帮我吗?
我user在Postgresql数据库中有一个表,如果运行User.count,它需要150ms获得结果.对我们来说太慢了.理想情况下,返回结果需要不到10毫秒.有没有办法在模型级别缓存sql结果?就像是
def self.total_count
User.count.cached # that's my imagination
end
Run Code Online (Sandbox Code Playgroud) 我不知道wtf是怎么回事,但我刚创建了一个新的Rails应用程序,无法让任何rake任务工作或显示在 $ rake -T
LIB /任务/ hello.rake
namespace :hello do
desc "hello"
task :you do
puts "hello"
end
end
Run Code Online (Sandbox Code Playgroud)
$ rake -T
它没有出现在那里
$ rake hello:you
耙子流产了!不知道如何构建任务'你好:你'(参见--tasks)
的Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.2'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets …Run Code Online (Sandbox Code Playgroud) 我有用户、组和成员资格。我使用成员资格联接表在用户和组之间建立了 has_many 关系。我想要做的是设置一个 after_create 回调,每当用户创建新组时,该回调都会将新记录添加到成员资格表中,这样创建者就自动成为该组的成员。
现在,用户可以创建群组,但该群组将显示有 0 名成员,并且用户必须加入该群组。如果我尝试从组控制器更新成员资格模型,如何通过回调自动执行该部分?
用户.rb
class User < ApplicationRecord
has_many :created_groups, class_name: "Group"
has_many :memberships
has_many :groups, through: :memberships
end
Run Code Online (Sandbox Code Playgroud)
组.rb
class Group < ApplicationRecord
belongs_to :creator, class_name: "User"
has_many :memberships
has_many :users, through: :memberships
end
Run Code Online (Sandbox Code Playgroud)
会员资格.rb
class Membership < ApplicationRecord
belongs_to :user
belongs_to :group
end
Run Code Online (Sandbox Code Playgroud)
组控制器.rb
class GroupsController < ApplicationController
after_create :set_membership
def create
@group = current_user.groups.build(group_params)
respond_to do |format|
if @group.save
format.html { redirect_to @group, notice: 'You have a new Group!' }
else
format.html …Run Code Online (Sandbox Code Playgroud) 我正在使用一个名为text_messages.js我希望仅在我的text_messages\new.html.erb页面中使用的javascript文件。
现在,当我进入控制台中项目的任何其他页面时,都会收到错误消息:
之所以发生此错误,是因为我text_messages.js在每个页面中都在尝试运行该页面。谢谢!
activerecord ×2
ruby ×2
activemodel ×1
dbmigrate ×1
javascript ×1
postgresql ×1
rake ×1
rake-task ×1