rails-activerecord

Ruby on Rails Models. Why does a decimal{2,1} with scope 1 allow more than digit after the decimal point?

情到浓时终转凉″ 提交于 2019-12-10 12:22:47
问题 I'm having an issue with a table accepting too many digits after the decimal, despite defining it's precision and scope. rails generate model Hotel name:string 'rating:decimal{2,1}' class CreateHotels < ActiveRecord::Migration def change create_table :hotels do |t| t.string :name t.decimal :rating, precision: 2, scale: 1 t.timestamps end end end However, I am able to do the following. Hotel.create!(name: “The Holiday Inn”, rating: 3.75) Additionally, I have a rooms table (Room model), with t

Editing multiple rows in a table (updating multiple records)

元气小坏坏 提交于 2019-12-10 11:46:23
问题 I have a list of payment records in Payments#index. I've been asked to make that table directly editable and to have one button that saves all the updates. So far I've only partially managed to do this by wrapping each row around a form_for for that record like so: <tbody> <% @payments.each do |payment| %> <tr> <td><%= payment.client.trading_name %></td> <td><%= payment.date_of_payment %></td> <td> <%= form_for payment do |f| %> <%= f.text_field :amount %> <%= f.submit 'Save', class: 'table

Rails 4.2: Unknown Attribute or Server Error in Log

我的梦境 提交于 2019-12-10 10:58:13
问题 I have a form with a select_tag and options_from_collection_for_select that I can't seem to get to pass. In the view, when the upload id is set to uploadzip_id I get a 302 redirect and when it's set to uploadzip_ids , I get a Unknown Attribute error. I'm a bit confused as I have my relationship set up along with the foreign key. I do have another model with checkboxes called Uploadpdf that works fine. Here is the set up.. class Campaign < ActiveRecord::Base has_one :uploadzip end class

rails migration: postgresql for md5 of random string as default

一世执手 提交于 2019-12-10 10:22:08
问题 Rails 3 + postgresql I want to have a sha of a random string for a default value of a column. So, in my migration I have: t.string :uniqueid, default: md5(random()::text) However i can not get this to actually produce anything, I've used backticks, quotes,etc. From examples that I've seen it seems like that pg function only works in a SELECT statement. Is that accurate? Any ideas on how I could achieve this? Thanks 回答1: Rails will try to interpret this: t.string :uniqueid, default: md5(random

Circular dependency detected while autoloading a constant

为君一笑 提交于 2019-12-10 10:06:19
问题 I upgraded Rails from version 3.1.2 (which worked fine) to 4.0, and got stuck with the following error: circular dependency detected while autoloading constant Foo I created a class ProductFactory , where I instantiate different models. For example: p = Foo.new(params) The model "Foo" is not always an ActiveRecord. Could anyone help me with this issue? 回答1: This kind of issues often happen when you change the version of Rails. You maybe didnt update the gems on the right order. 回答2: Best I'm

Rails 3 ignore Postgres unique constraint exception

…衆ロ難τιáo~ 提交于 2019-12-10 02:41:45
问题 What's the correct way to rescue an exception and simply continue processing? I have an app that has Folders and Items, with a habtm relationship through a join table called folders_items. That table has a unique constraint ensuring that there are no duplicate item/folder combinations. If the user tries to add an item to the same folder several times, I obviously don't want the additional rows added; but I don't want to stop processing, either. Postgres automatically throws an exception when

Negate ActiveRecord query scope

别来无恙 提交于 2019-12-10 02:29:05
问题 I have a slightly complicated scope on a model class Contact < ActiveRecord::Base scope :active, -> { where(inactive: false) } scope :groups, -> { where(contact_type: 2308) } scope :group_search, -> (query) do active.groups.where("last_name LIKE '%' + ? + '%'", query) end end For testing purposes, I want to make sure that all Contacts not returned by group_search are excluded for the right reasons. But to get that list, I have to load Contact.all - Contact.group_search('query') which runs two

Retrieve the nextval from a sequence using activerecord in Ruby on Rails 3.2.14 / Ruby 2.0.0 / PostgreSQL 9.2.4

时光毁灭记忆、已成空白 提交于 2019-12-10 01:44:10
问题 This should be SO simple. I want to retrieve the nextval of a sequence... it's not a default value... it's not a primary key... it's not a foreign key. In rare cases, I need a sequence number for a user supplied value. I've tried the following: @nextid = ActiveRecord::Base.connection.execute("SELECT nextval('xscrpt_id_seq')") and what I get back is: #<PG::Result:0x007fe668a854e8 @connection=#<PG::Connection:0x00000003aeff30>> And by using @nextid[0]["nextval"] I can get the correct value, but

has_one nested attributes not saving

自闭症网瘾萝莉.ら 提交于 2019-12-09 18:59:01
问题 I have two models Project and ProjectPipeline. I want to create a Project form that also has fields from the ProjectPipeline model. I have created the form successfully but when I hit save the values aren't stored on the database. project.rb class Project < ActiveRecord::Base has_one :project_pipeline accepts_nested_attributes_for :project_pipeline self.primary_key = :project_id end projectpipeline.rb class ProjectPipeline < ActiveRecord::Base belongs_to :project, autosave: :true validates

validate and update single attribute rails

一曲冷凌霜 提交于 2019-12-09 05:39:14
问题 I have the following in my user model attr_accessible :avatar, :email validates_presence_of :email has_attached_file :avatar # paperclip validates_attachment_size :avatar, :less_than => 1.megabyte, :message => 'Image cannot be larger than 1MB in size', :if => Proc.new { |imports| !imports.avatar_file_name.blank? } in one of my controllers, I ONLY want to update and validate the avatar field without updating and validating email . How can I do this? for example (this won't work) if @user