我只是在学习编程,特别是Ruby on Rails。我目前面临的问题是,对于干净的代码和避免代码气味或其他问题,我怀疑用哪种方式来处理我的问题更好。
所以基本上,我使用users_controller来检查我的jwt是否有效:我解码它,提取user_id,然后在DB中通过她的id查找用户。
def login
user = decoded_user(permitted_params[:jwt])
render json: user
rescue ActiveRecord::RecordNotFound, JWT::DecodeError
render status
我有一个通过嵌套表单构建的具有has_many关系的模型:
class User < ActiveRecord::Base
has_many :properties
accepts_nested_attributes_for :properties, allow_destroy: true
def billing_address
debugger
properties.find_by(billing_address: true)
end
end
总体而言,关系和嵌套形式是有效的。但是,如果我在创建过程中调用方法billing_address,那么它将返回
我真的不知道如何描述这个问题,我从来没有遇到过这样的问题。我正在尝试建立一个"has_many : group“关联来处理对一个组的成员请求。其他行动(破坏、更新)似乎运作良好。每当我在控制器中启动create操作时,该对象就会显示它被传递给正确的params并创建关联。但是,它只创建一个与"1“id的组关联。我不知道如何清楚地解释或解决这个问题。我会在下面张贴我的日志和代码。
为了澄清我的问题:当数据持续到数据库(在create上)时,它为什么会“改变”呢?
Cliqs =群
日志:
Started POST "/cliqs/2/cliq_requests"
在我当前的rails程序中,当我使用诸如
user = User.find(10)
当没有用户使用ID=10时,我会出现异常,如下所示:
ActiveRecord::RecordNotFound: Couldn't find User with ID=10
我可以得到nil而不是引发异常,所以当我做一些类似的事情时:
unless user = Challenge.find(10)
puts "some error msg"
end
当没有记录并且我不想使用begin/rescue时,我只想得到nil
谢谢
我有三个模特:
class User < ActiveRecord::Base
has_many :surveys
end
class Survey < ActiveRecord::Base
belongs_to :user
has_many :survey_sessions
end
class SurveySession < ActiveRecord::Base
belongs_to :survey
end
我的问题是:
如果有可能的话:
Survey.find_by!(id: params[:id], user: current_user)
为
在我的模型中,我首先检查数据库中是否存在Sudoku记录,然后才取其为solution或创建它。
if Sudoku.exists?(puzzle: puzzle)
return Sudoku.find_by(puzzle: puzzle).solution
else
**solve it and save it into the db**
end
这样做有什么区别:
if Sudoku.find_by(puzzle: puzzle)
return Sudoku.find_by(puzzle: puzzle).solution
else
**solve it and save
我是rails开发的新手,我一直在使用rails控制台来适应它。当我试图通过.find_by_id()找到一个记录时,它会抛出一个方法丢失的错误。
似乎active record gem有问题,但是我运行了gem update,它更新了活动记录gem,并且仍然得到相同的错误.我不知道到底是什么问题。
帮助是非常感谢的!!
NoMethodError: undefined method `find_by_id' for #<Class:0x007fdd03515238>
from /usr/local/lib/ruby/gems/2.2.0/gems/acti
我有下面的表TodoList:
class TodoList < ActiveRecord::Base
end
和
class CreateTodoLists < ActiveRecord::Migration
def change
create_table :todo_lists do |t|
t.string :list_name
t.date :list_due_date
t.timestamps null: false
end
end
end
我创建crud方法:
def create_todolist(par
在测试控制台中的两个表之间的关联时,我正面临上述问题。
我的模特是这样的:
class Team < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
我正在支持RoR上的一个应用程序。其中有一部分我不明白。有一个模型: class Country < ActiveRecord::Base
extend FriendlyId
friendly_id :prefix, :use => [:finders]
...
end 如果我使用数据库中不存在的值调用Country.find(" value ")或Country.friendly.find("value"),该调用会立即引发HTTP302。如果我将其更改为Country.find_by(前缀:"value"),它将按预期工
我的模特长得像这样
require 'bcrypt'
class Authorization < ActiveRecord::Base
before_save :encrypt_password
# some validations and other methods
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_encrypted = BCrypt::
post = Post.find 666
ActiveRecord::RecordNotFound: Couldn't find Post with 'id'=666
后=>零
post = "Banana"
post = Post.find 666
ActiveRecord::RecordNotFound: Couldn't find Post with 'id'=666
发布=>“香蕉”
如果查找不返回任何内容,我想确保post是nil。如果空手而归,是否有一种优雅的分配零的方法?
我的Rails应用程序中的以下代码(Ruby2.1,Rails 4.0.2)返回
a = Order.select('if(currency1_id=3,unitprice,1/unitprice) as uu,sum(if(currency1_id=3,open_quantity,open_quantity*unitprice)) as qq').where("((currency1_id=? and currency2_id=? and otype = 0) or (otype = 1 and currency1_id=? and currency2_id=?))&
class Recipe < ActiveRecord::Base
def self.tagged_with(name)
recipes = Tag.find_by(name: name).recipes
end
end
在控制器中
def tag
@recipes = Recipe.tagged_with(params[:tag])
render 'index'
end
路线
get 'tag/:tag', to "recipes#tag"
我如何保护这个方法不被破坏?如果我搜索一个还没有被创建的标签,我会得到一个