rails-activerecord

How do I add each price to the current price?

跟風遠走 提交于 2019-12-12 23:05:38
问题 I am trying to write a method that will find each price and add it to the current price. Quote.rb: class Quote < ActiveRecord::Base has_many :items def calc_price sum = 0 items.each do |item| item.price end sum = (item1 + item2 etc) end end 回答1: Here's a nice feature about the more current Rubies: values = [1,2,3,4,5] values.inject(:+) # => 15 Now, that said, you're working with a database, so have it sum the records. From the documentation: Calculates the sum of values on a given column. The

ActiveRecord boolean validation accepting non-boolean values

邮差的信 提交于 2019-12-12 15:44:27
问题 I'm trying to validate that an attribute is a boolean, i.e. true or false. From the Rails guide I expected validates :new_out_of_stock, inclusion: { in: [true, false] } to work, but it accepts non-boolean values (e.g. "Hi") as valid: irb> ip = ItemPrice.new irb> ip.new_out_of_stock = "Hi" => "Hi" irb> ip.valid? => true irb> ip.errors.messages => {} It correctly rejects nil and accepts true and false. All the documentation on the Internet that I've read says that this is the correct way to

RGeo error attempting to create point from lat/lon

為{幸葍}努か 提交于 2019-12-12 14:05:54
问题 I'm writing an app which I am attempting to integrate some general geolocation functionality including saving some lat/lon coordinates as a point in a database. The intention of this is to allow the user to either use their location (provided by device) or a google map marker to choose their coordinates. I have succeeded at obtaining the coordinates from the map or device, however I am having trouble now creating a point from them to save into the db. I've read the documentation and tried

Active record. Model.Find.last

China☆狼群 提交于 2019-12-12 12:02:11
问题 I have an ActiveRecord relationship between Trade and Execution . I can get Trade.executions #returns all exeuctions realated to the Trade If I do Trade.executions.last It seems seems to return the last execution record based on ID. Is this the correct way to retrieve the last execution record related to Trade based on ID? 回答1: No, that's not guaranteed to give you the Execution with the highest id . If you don't specify an explicit ordering then the records can come out of the database in

PostgreSQL jsonb field in ActiveRecord belongs_to association

北城以北 提交于 2019-12-12 10:35:42
问题 Can I use a jsonb field as a foreign_key in a belongs_to association? Something along the lines of: belongs_to :product, class_name: "Product", foreign_key: "data ->'product_id'" 回答1: References can be defined only between columns (or groups of columns), as it is stated in the documentation: FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ] 回答2: you can use activerecord-json-associations to use PostgreSQL JSONB fields to store association information of your

Rails: Polymorphic assosiation and accepts_nested_attributes

烈酒焚心 提交于 2019-12-12 10:19:55
问题 The attachment won't save. What I am missing here? In my application I have a project ,for each project user can upload many assets. The Upload is done by carrier wave. here are the models class Project < ActiveRecord::Base has_many :assets,:as => :assetable,dependent: :destroy accepts_nested_attributes_for :assets, :allow_destroy => true end class Asset < ActiveRecord::Base belongs_to :project belongs_to :user belongs_to :assetable, :polymorphic => true mount_uploader :attachment,

Adding parameter to a scope

有些话、适合烂在心里 提交于 2019-12-12 07:29:50
问题 I have a ActiveRecord query for example like this: @result = stuff.limit(10) where stuff is a active record query with where clauses, order by, etc... Now I thought why to pass magic numbers like that to the controller? So do you think is it a good practice to define a scope for "limit(10)" and use that instead? and how would the syntax look like? 回答1: The scope would look like any other (although you may prefer a class method), e.g., class Stuff < ActiveRecord::Base def self.lim limit(3) end

Rails complex has_many and has_many through

拟墨画扇 提交于 2019-12-12 06:12:00
问题 I have 4 models: Drawer, Folder, FolderDocument and Document as follows: class Drawer < ActiveRecord::Base has_many :folders #Drawer has many folders end class Folder < ActiveRecord::Base belongs_to :drawer has_many :folder_documents # Folder has a "version" attribute which reflects the latest version # Use proc to give back latest version by default e.g. folder.documents or folder.documents(5) will give back a specific version. has_many :documents, :through => :folder_documents, :conditions

Rails Active Record Query joins only, any

淺唱寂寞╮ 提交于 2019-12-12 04:21:42
问题 Student has_many :enrollments With this query I see those students that have true enrollments, but also those that have both , true and false enrollments: @students = Student.joins(:enrollments).where(enrollments: { is_active: false }) Is there some "only" attribute that I can add to see students that have only active enrollments? 回答1: One way straightforward would be to find the students that inactive enrolments and then explicitly exclude them. Something like: have_inactives = Enrollment

ActiveModel search by association

懵懂的女人 提交于 2019-12-12 04:08:21
问题 I've seen similar questions but nothing exactly like mine. I apologize if this is a duplicate - if it is, please refer me to answer(s). I need to search orders by customer names, and the link between the two is users. Here are my models: class Customer < ApplicationRecord belongs_to :user end class User < ApplicationRecord has_one :customer has_many :orders end class Order < ApplicationRecord belongs_to :user end I'm trying to search using: @orders = Order.joins(:user).joins(:customers).where