rails-activerecord

Model belongs_to eiher/or more than one models

 ̄綄美尐妖づ 提交于 2020-01-14 19:58:28
问题 Is it possible to have a model which belongs_to (either/or) more than one models? For example, in my project I have a subscription model that may belong to a person or a group. When a person will join a particular group she automatically "inherits" the subscriptions of that group. I have set up the following associations In person.rb: has_many :subscriptions In group.rb: has_many :subscriptions In subscription.rb: belongs_to :person belongs_to :group Also, I have added fields for person_id

Rails ActiveRecord Connecting to Wrong Postgres Database

被刻印的时光 ゝ 提交于 2020-01-14 06:22:36
问题 My database.yml is as follows: development: adapter: postgresql database: phunt_development username: <%= ENV['PG_USER'] %> password: <%= ENV['PG_PASS'] %> host: localhost pool: 5 timeout: 5000 And I can confirm that the database phunt_development does exist. However when I run ActiveRecord::Base.connection.current_database I get development not phunt_development . I can't figure out why Rails is insisting on connecting to the general development database, which is polluted with data I have

Convert Rails 4 has_many from condition with proc to where

主宰稳场 提交于 2020-01-13 09:17:29
问题 I have the following working has_many with a proc to capture a parameter for context: has_many :subclass_point_analytics, :conditions => proc {"assessment_id = #{self.send(:assessment_id)}" }, :foreign_key => 'gid', :dependent => :destroy I am using Rails 4 and it is (rightfully) complaining about use of :conditions. After 30 minutes and lots of tries I cannot figure out how to convert :conditions to -> { where ... } format. I would appreciate someone with knowledge of proc syntax to help me

undefined method `explain' for #<ActiveRecord::ConnectionAdapters::MysqlAdapter

瘦欲@ 提交于 2020-01-12 19:14:09
问题 I'm new to Ruby on Rails, but I have followed some tutorials and know my way around a little bit. I have generated some scaffolding and inserted data into a MySql database. When navigating to index.html.erb I receive the error in the title The controller is executing index def index @beers = Beer.all respond_to do |format| format.html # index.html.erb format.json { render :json => @beers } end end And has this as a structure Beer: id, brewer_id, name, price, score, color, brew_type, create_at

undefined method `explain' for #<ActiveRecord::ConnectionAdapters::MysqlAdapter

爱⌒轻易说出口 提交于 2020-01-12 19:12:29
问题 I'm new to Ruby on Rails, but I have followed some tutorials and know my way around a little bit. I have generated some scaffolding and inserted data into a MySql database. When navigating to index.html.erb I receive the error in the title The controller is executing index def index @beers = Beer.all respond_to do |format| format.html # index.html.erb format.json { render :json => @beers } end end And has this as a structure Beer: id, brewer_id, name, price, score, color, brew_type, create_at

Using raw sql queries in Rails 3 application?

烈酒焚心 提交于 2020-01-12 16:10:36
问题 I am working on migrating a legacy database into my Rails application (3.2.3). The original database comes with quite a few long sql queries for reports. For now, what I would like to do it use the sql queries in the Rails application and then one by one (when time allows) swap the sql queries to 'proper' Rails queries. I have a clinical model and the controller has the following code: @clinical_income_by_year = Clinical.find_all_by_sql(SELECT date_format(c.transactiondate,'%Y') as Year, date

Using raw sql queries in Rails 3 application?

浪子不回头ぞ 提交于 2020-01-12 16:10:04
问题 I am working on migrating a legacy database into my Rails application (3.2.3). The original database comes with quite a few long sql queries for reports. For now, what I would like to do it use the sql queries in the Rails application and then one by one (when time allows) swap the sql queries to 'proper' Rails queries. I have a clinical model and the controller has the following code: @clinical_income_by_year = Clinical.find_all_by_sql(SELECT date_format(c.transactiondate,'%Y') as Year, date

Using Delegate With has_many In Rails?

跟風遠走 提交于 2020-01-11 05:13:25
问题 We've got 2 models & a join model: #app/models/message.rb Class Message < ActiveRecord::Base has_many :image_messages has_many :images, through: :image_messages end #app/models/image.rb Class Image < ActiveRecord::Base has_many :image_messages has_many :messages, through: :image_messages end #app/models/image_message.rb Class ImageMessage < ActiveRecord::Base belongs_to :image belongs_to :message end Extra Attributes We're looking to extract the extra attributes from the join model (

DateTime arithmetic in an ActiveRecord query (PostgreSQL)

时间秒杀一切 提交于 2020-01-10 04:08:15
问题 I have two datetime columns in a User / users table: created_at and birthdate . I'd like to find users whose birthdate is less than 13 years before their creation date. The equivalent Rails if statement would be ((created_at - birthdate) < 13.years) How do I translate this to an ActiveRecord query (one that'll work on PostgreSQL)? Is this even possible, or will I have to iterate over all records manually? 回答1: The easiest way to do this is to use an interval, then it is pretty much a straight

Limit simple_form_for associated records number in Ruby on Rails

牧云@^-^@ 提交于 2020-01-07 03:39:08
问题 I have a large psychological test of 251 question. Each user can complete that test many times. So I created Summary model to represent each completion. Each Summary has many Answers. For each Summary I created a form that represents collection of answers, using Slim templater and simple_form gem: = simple_form_for(@summary) do |f| = f.simple_fields_for :answers do |a| .question = a.input :question_id, as: :hidden div= a.object.question.title - if a.object.question.kind_of?