Tag: rails activerecord

如果从特定路由调用创建方法,如何检查validation?

如果从其他路径调用创建方法,我想在创建方法上validation列存在。 例如,如果我有以下两条路线: post ‘create_item’, to: ‘item#create’ post ‘create_verified_item’, to: ‘item#create_verified’ 我需要在Item模型中定义这样的东西: validates :verified_number, presence: true, if: Item.action_name == “create_verified” 有人可以帮忙吗?

通过以下方式进行多对多:从Associations :: CollectionProxy到AssociationRelation

通过与客户关系密切的客户关系(通过favorites.rb)我有多对多的关系,我想知道如何将CollectionProxy转换为AssociationRelation,这样我就可以获得与用户最喜欢的所有客户的关系。 或者只是简单地说,如何在AssociationRelation中获取它们 – 不必从CollectionProxy转换。 编辑:我正在寻找的只是一个列出所有客户的关系。 我意识到我的问题不同。 我如何与所有客户建立关系? – 不是与favorite_id,user_id等的关系 – 我想要一个纯粹与客户及其行的关系。 我可以像这样找到CollectionProxy: user = User.last user.favorites # Gives me a CollectionProxy 但是我如何找到最喜欢的客户作为AssociationRelation? 楷模: user.rb: has_many :favorites, :dependent => :destroy has_many :clients, through: ‘favorites’ Client.rb has_many :favorites, :dependent => :destroy has_many :users, through: ‘favorites’ Favorite.rb belongs_to :user belongs_to :client

处理rails中的关联为has_many通过…但是两个连接表深

编辑4.在我的解释中的一些东西是不正确的…附件是一个(非常糟糕的图形),显示表格和架构是如何,然后我的主要问题是我有什么提供者通过加入通过provider_location表的组位置ID,组位置保存组,最终保存组名。 这就是我想知道的,构建一个这样的表如何获得组名? 从提供者那里一路走来。 这就像我需要一个has_many贯穿。 (大注:图像中的提供商地址和组地址实际上是提供商位置和组位置) 编辑x 3:谢谢@mrshoco。 它让我更接近,现在它就像我的结构其他东西不太正确….我在运行provider_test_location.rb时遇到此错误 vagrant@precise32:/vagrant/ipanmv2$ rake test test/models/provider_location_tes t.rb Run options: –seed 18117 # Running: E Finished in 0.190900s, 5.2383 runs/s, 5.2383 assertions/s. 1) Error: ProviderLocationTest#test_fetching_a_group_name_for_a_provider: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: provider_ locations.group_id: SELECT groups.group_name FROM “groups” INNER JOIN “provider_ locations” ON “provider_locations”.”group_id” = “groups”.”id” INNER JOIN “provid ers” ON “providers”.”id” […]

图像名称未插入到轨道上的ruby中的数据库中

我有一个问题是将图像名称存储到数据库中。 图像上传到文件夹工作正常,但图像名称不会保存到数据库中 型号代码: class Post directory # create the file path path = File.join(directory,name) # write the file File.open(path, “wb”) { |f| f.write(upload[‘imag’].read)} end end 控制器代码: def create @a=params[:post][:imag].original_filename /* how to pass in this image name into params[:post] */ pos= Post.save(params[:post]) if pos redirect_to :action =>”index” else redirect_to :action =>”posts” end end 有人指导我存档这个。 提前致谢。

表多元化?

我有3个表具有以下关系(为了简单和清晰,表已被修剪): create_table “users”, force: :cascade do |t| t.string “name”, limit: 255, null: false end create_table “sleeves”, force: :cascade do |t| t.integer “user_id”, limit: 4 t.integer “report_id”, limit: 4 end create_table “reports”, force: :cascade do |t| t.string “name”, limit: 255, null: false end 我的模特喜欢这样: class User < ActiveRecord::Base has_many :sleeves end class Sleeve < ActiveRecord::Base belongs_to […]

如何将数据从生产数据库迁移到开发数据库(Rails 4)?

我知道这听起来有些倒退! 我一直在研究基于云的案例管理应用程序,并且正在开发支持票务function。 我们的开发数据库(MySQL)拥有与生产数据库相同的所有数据(这是一个非常大的应用程序)。 开发基本上是一个“沙箱”环境,因此开发数据库与生产数据库完全相同。 今天早上,我在本地开发服务器中遇到了一个问题: Migrations are pending; run ‘bin/rake db:migrate RAILS_ENV=development’ to resolve this issue. 好吧,那样做,即使它没有任何意义。 它错了,因为它试图创建已经存在的表(我已经运行了我的迁移,function在一天前完成了!一切都很好)。 在我的开发服务器中收到此消息之前,我唯一能做的就是取消注释一个完全注释掉的文件来尝试修复TinyMCE(/config/tinymce.yml,文本编辑器)的问题,然后重启我的rails开发服务器。 所有这个文件都是一堆默认和插件。 当我看到这个问题时,我首先将它们全部评论回来。 还是一样的错误。 继续rake db:tasks。 开始寻找答案..不断出错。 犯了一个noob错误,最终做了一个 rake db:migrate:reset 我知道会以管理db的方式重建模式。 这失败了..但是我们确实在不止一个地方有数据库(因为它和生产一样)。另外,只是为了尝试将所有表格带回来: rake db:schema:load ….无论如何,在其中一张桌子上失败了 ActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `login` USING btree ON `users` (`login`, […]

ActiveModel按关联搜索

我见过类似的问题但没有完全像我的。 如果这是重复,我道歉 – 如果是,请转介我回答。 我需要按客户名称搜索订单,两者之间的链接是用户。 这是我的模特: class Customer < ApplicationRecord belongs_to :user end class User < ApplicationRecord has_one :customer has_many :orders end class Order < ApplicationRecord belongs_to :user end 我正在尝试搜索: @orders = Order.joins(:user).joins(:customers).where(‘last_name LIKE ?’, name[0]) 但我收到错误信息 – 无法将“订单”加入名为“客户”的协会; 也许你拼错了吗? 我确信我没有正确的协会,但我不知道该怎么办。 感谢您提供的任何建议。

Rails ActiveRecord:如何通过超级父视图获取非重复的子记录?

我有四张名为Albums , Songs , Singers和SongSingers 。 Albums { Id, Name } Songs { Id, AlbumId, Name } Singers { Id, Name } SongSingers { Id, SongId, SingerId } 专辑有很多歌。 歌曲属于专辑。 SongSinger属于Song and Singer。 歌曲和歌手有许多SongSingers。 每首歌可能有相同或不同的歌手。 在View中,如何通过AlbumId获取所有非重复的歌手。 谢谢。

如何在ActiveRecord对象上调用范围数组?

我有一个Article模型,它具有recent , compiled , featured范围。 我可以像这样链接这些范围: Article.recent.compiled.featured 现在我有一系列这些范围: scopes = [:recent, :compiled, :featured] or scopes = [:recent, :compiled] 我事先不知道scopes变量中有多少个范围。 我只知道它是一系列范围。 如何在上面提到的Article模型上调用此数组作为范围链?

使用Rails进行多选选择创建操作

我有一个Collection_select,Multiple设置为true #views/courses/new true,:size => 8,:class=> “text”)%> 在我的模型中 #deparment Model has_many :courses #Course Model belongs_to :deparment 我想要一种情况,如果课程有多个部门从多选列表中选择,则此详细信息将保存在课程表中。 我的当前实施只保存了第一个选定的课程部门,并丢弃其余部分。 请问我该如何做到这一点。 def create @course = Course.new(params[:course] || []) if @course.save redirect_to courses_path, :notice => “Course Created Successfully” else redirect_to new_course_path flash[:alert] = “Error Creating Course” end end 谢谢