activerecord

Ruby on Rails - Using callback to multiply two objects in separate tables when new entry is created

删除回忆录丶 提交于 2019-12-12 04:15:29
问题 I have 3 tables, Goals, Activities and Goal_Activities. Goal has a set value for xp and Activity takes an input from the user. Using the on_create method in Activity, I'd like to multiply the user input with the goal xp and store that in the Goal Activities table so I can keep a record of each update and eventually add them all together to create a total goal xp in the Goal table. I've found a couple of questions on SO, here and here, that cover similar topics but I'm finding it hard to get

when I try to do rake db:migrate, I get an error: ActiveRecord::NODatabaseError role “ubuntu” does not exist

为君一笑 提交于 2019-12-12 04:15:16
问题 I started the postgresql server by doing this: sudo service postgresql start then I connected to the service: sudo sudo -u postgres psql then I created a database (i'm trying to add a voting system to my app): postgres=# CREATE DATABASE "votes"; but i still have the same problem. Also, when I do rake db:create I get a role "ubuntu" does not exist error Here is my database.yml: # SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # #

How do I sort on an association of a specific user?

三世轮回 提交于 2019-12-12 04:14:01
问题 I have a Profile that can be published . A profile belongs_to :user and has_many :ratings . A User has_one :profile , and has_many :ratings . A Rating belongs_to :profile && belongs_to :user . These are the schemas for the above models: Profile.rb : # == Schema Information # # Table name: profiles # # id :integer not null, primary key # first_name :string # last_name :string # created_at :datetime not null # updated_at :datetime not null # user_id :integer User.rb : # == Schema Information #

rails 4 model association left join validate id

≡放荡痞女 提交于 2019-12-12 04:11:52
问题 I have been battling this for a bit now, I want to create a LEFT join condition, so I have a Coupon model, users can create coupons but they may or may not be assigned to a job, when they are a assigned they are considered executed. I had this setup as has_one :job on the coupon model with a add_reference :jobs, :coupon, index: true index, but this seems borked. I think my brain is fried today… If coupon is used I would ideally like to confirm that it is assigned to valid job, which is why I

Can I force Yii to use particular alias in generated SQL

╄→尐↘猪︶ㄣ 提交于 2019-12-12 04:09:49
问题 I'm retrieving my records using CActiveRecord with a with() statement: Probes::model()->with(array ( 'user', 'results', 'results.answer', 'survey', 'survey.questions' ))->findByPk($id) I wanted to use GROUP BY on question_id field of survey.questions relation, so I changed above to: 'survey.questions'=>array('group'=>'survey.questions.question_id'), This caused a SQL exception 1054 Unknown column . However, by analyzing attached SQL code: `questions`.`question_id` AS `t6_c2` I managed to find

Ambiguous table reference

筅森魡賤 提交于 2019-12-12 03:57:46
问题 This problem seems fairly simple, but I've never encountered one like this. Here are the settings: Post has_many :reader_links Post has_many :readers, :through => :reader_links I need to find out if there are readers reading a post. @post.reader_links.where('created_at >= ?', 45.minutes.ago).any? Works great. @post.readers.where('created_at >= ?', 45.minutes.ago),any? throws an ambiguous table column error because it's confused whether the created_at column means that of reader object or

How to retrieve objects that have an attached file and order those objects based on a set of pre-ordered values?

别来无恙 提交于 2019-12-12 03:47:44
问题 I am using Ruby on Rails 3.2.2 and Paperclip 2.7.0. I would like to retrieve data from the database where records/objects have an attached file (in my case it is an image file) and order those objects based on a set of pre-ordered values. That is, I have image_file_name (handled by the Paperclip gem) and status (status can be published , unpublished , blocked ) database table columns and I would like that: returned data contains primarily records/objects to which is associated an image (maybe

RoR: How to handle custom nested form's submit

↘锁芯ラ 提交于 2019-12-12 03:46:10
问题 I managed to do almost all the way towards happiness with my custom form in ruby-on-rails, but the very last step is missing and it is impossible to find the answer on the net because of too many common words. I believe that the answers to my questions are trivial for people who have done RoR for a time, but be warned that the presentation of the question will be somewhat complicated. Let's see an equivalent problem! Schema: publishers (id, name, address) books (id, title, publisher_id,

CodeIgniter ActiveRecord one row at a time

纵然是瞬间 提交于 2019-12-12 03:44:54
问题 I'm running a query that selects many rows. When I loop over the results, I want to fetch a single row at a time (since it's too much memory to fetch them all at once). Problem is, CodeIgniter's Active Record $query->row for all it's variations will fetch all the records from the database into the memory (and return a single row). I looked into the source of DB_result.php and found: public function row_object($n = 0) { $result = $this->result_object(); if (count($result) == 0) { return

Rails get unique country with cities

限于喜欢 提交于 2019-12-12 03:36:58
问题 How can I list unique country followed by related cities? Following were my product table: name country city p1 US New York p2 US Boston p3 US Chicago k1 UK London k2 UK Liverpool Controller: @countries = Product.joins(:user).distinct.where("country is not null and country <> ''").where(:users => {:merchant_status => 1}).pluck(:country) @cities = Product.joins(:user).distinct.where("city is not null and city <> ''").where(:users => {:merchant_status => 1}).pluck(:city) @countries.map! {