rails-activerecord

ruby on rails import csv from upload file to Active Records using Smarter csv

流过昼夜 提交于 2019-12-13 04:49:22
问题 I hit a roadblock here and need help. I want to be able to import csv file to my Active Record. Either with SmarterCSV or some other way Here is my database create_table "ques", force: true do |t| t.integer "rikt_nr" t.integer "start_nr" t.integer "end_nr" t.datetime "created_at" t.datetime "updated_at" end here is my view <h2>Import Ques</h2> <%= form_tag import_ques_path, multipart: true do %> <%= file_field_tag :file %> <%= submit_tag "Import" %> <% end %> here is my route resources :ques

How to associate existing model with new model in Rails

老子叫甜甜 提交于 2019-12-13 04:43:07
问题 I have a 'tip' model and a 'team' model. I am trying to create a form where a user will select the winning teams from several games. So far the user can select the winning teams and those id's are being saved as foriegn keys in the Tip model (team_id). But after saving, I can't go (for example ) @tip.team.name . What do I need to do ? How do I associate it (I though rails might magically do it if foriegn key is set), I am very new to rails. Thanks for any help ! class Tip < ActiveRecord::Base

Random default value for integer in database for each instance?

跟風遠走 提交于 2019-12-13 04:37:22
问题 I'm using this system for voting content in my rails app: https://github.com/twitter/activerecord-reputation-system Is there a way to make the default score for any votable item some random number for each instance. If I store something like rand(5..12) it will only pick a random default value one time, how do I get a random default value for every different row or field? create_table "rs_evaluations", :force => true do |t| t.string "reputation_name" t.integer "source_id" t.string "source

ActiveRecord Query of associated model Rails 3

别说谁变了你拦得住时间么 提交于 2019-12-13 04:21:46
问题 I have three models in rails, Project(date, has_many :project_savings), Usage(month, amount, has_many :project_savings) and MonthlyProjectSaving(amount, belongs_to :usages, :projects). It's set up so that each project has a number of savings which correspond to a number of usages months. I'm trying to find all the project savings which have a corresponding project.date >= usage.month , and also a usage.amount == 0 in the most secure way possible. usage.month and project.date are both date

Access named scope dynamically

ε祈祈猫儿з 提交于 2019-12-13 03:06:27
问题 If I have 3 named scopes like class Foo scope :test1, ... scope :test2, ... scope :test3, ... And a function def x(variable) end where variable is a string("test1","test2" or "test3") How can I access the named scope just by knowing that variable's value ? Something like Foo.variable 回答1: You would call Foo.public_send(variable) . 来源: https://stackoverflow.com/questions/15000854/access-named-scope-dynamically

Using oracle bind variables with select_all in Rails

有些话、适合烂在心里 提交于 2019-12-13 02:33:19
问题 I'm trying to take advantage of bind variables in Ruby on Rails, and it doesn't need to instantiate a model, so I'm using select_all like so ActiveRecord::Base.connection.select_all( 'select * from users where id = :test', {test: 'foo'} ) But I get this big old error: ActiveRecord::StatementInvalid: OCIError: ORA-01008: not all variables bound: select * from users where id = :test from stmt.c:243:in oci8lib_220.so from /usr/local/rvm/gems/ruby-2.2.2/gems/ruby-oci8-2.2.4.1/lib/oci8/cursor.rb

Ruby on Rails - what is make method on a model?

邮差的信 提交于 2019-12-13 01:53:46
问题 In my lib/tasks/data.rake, I have User.make(attributes). The make method appears to create a new record, but I cannot find any docs on this. The make method is not defined by my User model and fyi my User model inherits ActiveRecord::Base. 回答1: I 'called a friend', it turned out to be https://github.com/notahat/machinist gem 来源: https://stackoverflow.com/questions/14451397/ruby-on-rails-what-is-make-method-on-a-model

Nested routes and CRUD operations with additional values for assciation in Rails

偶尔善良 提交于 2019-12-13 01:19:34
问题 I created one has_many through relationship in rails api. I also used nested routes. My models like below; class Author < ActiveRecord::Base has_many :comments has_many :posts, :through => :comments end class Post < ActiveRecord::Base has_many :comments has_many :authors, :through => :comments end class Comment < ActiveRecord::Base belongs_to :author belongs_to :post end my routes like below; Rails.application.routes.draw do namespace :api, defaults: { :format => 'json' } do resources :posts

Need to convert a Boolean from Postgres (== String) to a Ruby Boolean

萝らか妹 提交于 2019-12-13 00:29:18
问题 I'm using Postgres with Rails. There's a query with a subselect which returns a boolean, but Postgres always returns a String like 't' or 'f' . But in the generated JSON I need a real boolean. This is my query: SELECT *, EXISTS ( SELECT TRUE FROM measurement JOIN experiment ON measurement.experiment_id = experiment.id JOIN location ON experiment.location_id = location.id WHERE location.map_id = map.id LIMIT 1 ) AS measurements_exist FROM "map" It doesn't matter whether I use THEN true or THEN

How to check validation on creation method if its called from specific route?

醉酒当歌 提交于 2019-12-12 23:42:00
问题 I want to validate column presence on creation method if the creation method is called from some other route. For example, If I have following two routes: post 'create_item', to: 'item#create' post 'create_verified_item', to: 'item#create_verified' I need to define in Item model something like this: validates :verified_number, presence: true, if: Item.action_name == "create_verified" Anyone can help? 回答1: Ideally you can add a attribute to item to check that, something like: # model class