Tag: rails models

从范围记录计算总数百分比并显示虚拟属性(Rails / Active Record)

创建一个范围,可能是这样的.. scope :mv, select(‘*,quantity*market_price as market_value, quantity*market_price/sum(quantity*market_price) as percent’) 这会创建两个虚拟属性,market_value和percent。 我遇到的问题是创建包含sum()的百分比。 如果我添加sum(),则范围返回一条记录。 我需要计算相对于范围记录集的总市值的market_value的百分比。 例: 1, market_value: 100, percent: .10 2, market_value: 100, percent: .10 3, market_value: 100, percent: .10 4, market_value: 100, percent: .10 5, market_value: 100, percent: .10 6, market_value: 500, percent: .50 Total is 1000 但是,如果我将其范围扩大到market_value <6,我应该看到 1, market_value: 100, percent: .20 2, […]

Rails:属于另外两个模型的新入口

在我的应用程序中,产品有很多意见,由许多客户编写。 以下是具有关联的模型: class Product < ActiveRecord::Base attr_accessible :name, :desc, :price has_many :opinions end class Client < ActiveRecord::Base attr_accessible :name, :bio has_many :opinions end class Opinion < ActiveRecord::Base attr_accessible :rate, :comment belongs_to :client, :product end 在参数中,我有评估邀请ID,这有助于我同时获得product_id和client_id(所以考虑我同时拥有它们)。 表单仅包含费率(radio_button,从1到5)和注释(text_field)。 方法意见#create如何创建新意见,属于两种模式:客户端和产品? 我试图直接传递client_id和product_id,但是我收到了MassAssignment错误: # Remember: I have product_id and client_id product = Product.find_by_id product_id opinion = product.opinions.build params[:opinion] opinion.product_id = […]

Ruby on rails:将单个模型的2个引用添加到另一个模型

我想知道在rails 4.0的ruby中实现我的情况的正确方法。 可以说我有两个名为House and Order的模型。 我的订单表应该有两列引用房屋模型。 在这种情况下,我这两个模型之间的关系应该是什么? 注意:我不需要从房屋模型引用订单模型。 我希望在我的Order表中有这样的东西 t.references :house, as:from (this should create a column named from and should be of type integer, index of house table t.references :house, as:to (this should create a column named to and should be of type integer, index of house table 我想在订单模型中使用这种类型的关系,因为我想在我的订单中采用类似的房屋 … # order fields … […]

关于Rails上的关联和模型的问题

我最近一直致力于一个项目,玩家可以创建一个团队并成为团队所有者,但是一个玩家也可以通过一个名为Squad的单独表格成为团队的一员。 class Player has_many :teams has_many :squads end class Squad belongs_to :player belongs_to :team end class Team belongs_to :owner, :class_name => “Player” has_many :squads has_many :players, :through => “squads” end 我不知道这是否是我需要做的我想要的东西,但我无法弄明白。 如何让玩家通过Squad邀请加入团队,团队的所有者对该玩家回答是或否? 如果是的话,他加入了Squad表,并且是团队的一员。 如果不是,他的请求就会被销毁。

使用模型中的方法,从视图中调用它

我有一个属于用户的更新模型。 要显示一个用户的所有朋友的更新,我正在执行以下操作: Update.where(“user_id” => [array_of_friend_ids]) 我知道“正确”的做法是创建一个方法来创建上面的数组。 我开始编写方法,但它只是半工作。 目前我在我的用户模型中有这个: def self.findfriends(id) @friendarray = [] @registered_friends = Friend.where(“user_id” => id) @registered_friends.each do |x| @friendarray << x.friend_id end return @friendarray end 我在视图中执行整个操作: <% @friendarray < 然后我正在调用Update.where(“user_id”=> @friendarray)。 但显然我在这里以非常黑客的方式做事。 当Rails可以从视图中的模型和方法“看到”某些变量时,我有点困惑。 插入ID数组以查找更新的最佳方法是什么,因为我不应该在视图中使用太多逻辑?

使用从其他(mongoid / rails)inheritance的名称空间和引用模型的正确方法是什么?

我有从Scenarioinheritance的模型HandlingScenario 。 像这样: ## models/scenario.rb class Scenario include Mongoid::Document end ## models/scenarios/handling_scenario.rb class Scenarios::HandlingScenario :handling_scenarios # In the `User` model, the reference is `has_many :handling_scenarios` end 但是当我尝试访问HandlingScenario类时,我遇到了麻烦: ➜ rails c Loading development environment (Rails 3.2.12) 2.0.0-p247 :001 > User.first.handling_scenarios LoadError: Expected /Users/christoffer/Projects/my_project/app/models/scenarios/handling_scenario.rb to define HandlingScenario 此外,当我尝试通过浏览器访问时,我收到此错误: Started GET “/scenarios/handling_scenarios” for 127.0.0.1 at 2013-10-06 19:41:29 +0200 […]

Ruby on Rails – 在用户单击create action时将保存的字符串连接到数据库

它来晚了,我可能应该睡在这上面。 容易一个 我在一个用户填写的表单中有3个字段。一旦他们单击创建按钮,这些记录就会保存到数据库中。 简单。 但是,我希望同时将这三个字段中的数据连接在一起,没有什么花哨的……并且与其他记录同时插入到数据库中。 这应该在创建后反映给显示页面上的用户。 所以我需要一个动作来连接3个db列,让我们说列名是firstname,surname和DOB。 表名PeopleDetails 我尝试使用after_create,before_save构建模型,内置到模型中,但是nada。 建议。 我想我会在睡了一会儿后再来看看

Rails从另一个模型的控制器更新一个模型中的数据

我有一个具有billing_id的用户模型。 我有订单模型,它使用支付网关运行交易,支付网关返回我想要保存到用户模型的billing_id列中的账单ID。 我想我将MVC架构的基础知识搞混了。 我的印象是UsersController和OrdersController更新了各自表的数据。 这是否意味着如果我从OrdersController返回了某些内容,例如结算ID,那么没有其他方法可以将该结算ID保存到用户模型的billing_id列中? 如果这是非常简陋的话,谢谢和抱歉。 🙂 另外,我认为可能的解决方案可能是以某种方式通过ApplicationsController将返回值传递给UsersController以保存到User表中。 这可能吗?

has_one和has_many在同一个模型中。 铁轨如何跟踪它们?

即使它正常工作,我对这项工作有点困惑。 我有一个与同一个其他模型有两个关联的模型。 公司拥有一个所有者和公司拥有许多类用户的员工。 这是我的公司型号: class Company ‘User’ has_one :owner, :class_name => ‘User’ accepts_nested_attributes_for :owner, :allow_destroy => true end 这是我的用户模型: class User < ActiveRecord::Base include Clearance::User attr_accessible :lastname, :firstname #other attr are whitelisted in clearance gem validates_presence_of :lastname, :firstname belongs_to :company end 现在假设我有这家公司的3名员工,包括业主。 当我第一次创建公司时,我将所有者设置为id为1的员工,另外两个(2,3)通过设置company_id(user.company = company)添加到员工列表中。 所有这三个都将他们的company_id设置为公司ID,我们可以假设为1 当我要求company.owner时,我得到了正确的用户,当我做公司工作时,我得到了所有三个。 如果我将所有者更改为用户2,则会通过将用户1的company_id设置为nil来自动从员工中删除用户1。 这很好,如果我把他作为一个简单的员工加回来,一切都还是不错的。 铁路怎么知道哪个是哪个? 我的意思是,它是如何知道员工是所有者而不仅仅是员工? 架构中没有任何内容定义此内容。 我有一种感觉,我应该扭转所有者协会,让公司属于一个用户。

如何将序列添加到迁移并在模型中使用它们?

我希望拥有一个普通主键的“ Customer ”模型和另一个存储自定义“客户编号”的列。 另外,我希望db处理默认的客户编号。 我认为,定义序列是最好的方法。 我使用PostgreSQL。 看看我的迁移: class CreateAccountsCustomers true t.integer :salutation, :limit => 1 t.string :cp_name_1 t.string :cp_name_2 t.string :cp_name_3 t.string :cp_name_4 t.string :name_first, :limit => 55 t.string :name_last, :limit => 55 t.timestamps end say “Adding NEXTVAL(‘customer_no_seq’) to column cust_id” execute “ALTER TABLE accounts_customers ALTER COLUMN customer_no SET DEFAULT NEXTVAL(‘customer_no_seq’);” end def down drop_table […]