activerecord

Rails: Records are not ordered by id in heroku

删除回忆录丶 提交于 2020-01-17 05:12:52
问题 Im having a problem I developed my app in my local environment and everything works as espected, but after I deployed to heroku something is not working right. I have 2 models PurchasingGroup and GroupGoal a purchasing group has many group goals, well when I create group goals for a purchasing group I can check in the console like this PurchasingGroup.last.group_goals and the result is this one [#<GroupGoal id: 130, created_at: "2015-03-25 17:09:08", updated_at: "2015-03-25 17:10:37", other

Active Record Rails 3 associations not working properly

徘徊边缘 提交于 2020-01-17 03:45:14
问题 I have the following classes with associations: class Customer < ActiveRecord::Base has_many :orders has_many :tickets, :through => :orders has_many :technicians, :through => :ticket has_many :services, :through => :ticket end class Order < ActiveRecord::Base belongs_to :customer has_many :tickets has_many :technicians, :through => :tickets has_many :services, :through => :tickets end class Service < ActiveRecord::Base has_many :tickets has_many :technicians, :through => :tickets has_many

PHP Activerecord Model Count Associations

纵饮孤独 提交于 2020-01-17 03:04:04
问题 I have three tables: Member MemberBranch Branch The Member can have many Branch es through MemberBranch . And a Branch can have many Member s etc. What I want to be able to do is get a count of how many members a branch has. so $branch = Branch::find_by_title('London'); $branch->number_of_members; // Will equal how many members have the branch through MemberBranch How would I go about doing this? 回答1: Try this: SELECT B.*, (SELECT count(*) as total from MembersBranch where branch_id = B.id)

Codeigniter update_batch is not working

Deadly 提交于 2020-01-17 01:34:25
问题 I have a db table: ConfigID | Type | Key | Value -------------------------------------------------------------- 0 | "API" | "ClientID" | "iofoewi" 1 | "API" | "ClientSecret" | "eijfoiewjfioejfoiewjfoie" Take the following code: $data = array( array( 'Key' => "ClientID", 'Value' => $testAPICredential->ClientID ), array( 'Key' => "ClientSecret", 'Value' => $testAPICredential->ClientSecret ) ); try { $this->context->db->trans_start(); $this->context->db->update_batch( $this->tableName, $data,

How to run calculations on sum of two columns?

别来无恙 提交于 2020-01-17 00:01:41
问题 In the Invoice class of my Rails application I need to find all invoices that are overdue. I have only two database columns, date (which is a type datetime field) and days_allowed (which is a type integer field). This is what I've got: class Invoice < ActiveRecord::Base def self.overdue where("date + days_allowed < ?", Date.today) end end It's neither throwing an error nor returning the relation that I need, though. Is there a better way to sum two database columns and then do calculations on

How to run calculations on sum of two columns?

随声附和 提交于 2020-01-16 23:58:24
问题 In the Invoice class of my Rails application I need to find all invoices that are overdue. I have only two database columns, date (which is a type datetime field) and days_allowed (which is a type integer field). This is what I've got: class Invoice < ActiveRecord::Base def self.overdue where("date + days_allowed < ?", Date.today) end end It's neither throwing an error nor returning the relation that I need, though. Is there a better way to sum two database columns and then do calculations on

Rails Trying to create a profile after the user has been created

試著忘記壹切 提交于 2020-01-16 20:10:26
问题 I'm trying to figure out how to create a new profile for the user that has just been created, I'm using devise on the User model, and the User model has a one to one relationship with the UserProfile model. Here's what my User Model looks like: class User < ActiveRecord::Base has_and_belongs_to_many :roles belongs_to :batch has_one :user_profile after_create :create_user_profile def create_user_profile self.user_profile.new(:name => 'username') end # Include default devise modules. Others

Rails 4 active record validation - conditionally validate presence of 4 attributes if at least one is present while allowing none to be present

末鹿安然 提交于 2020-01-16 18:07:07
问题 I have a form with 10 attributes. Among them I have 4 attributes which I need to apply what I'd call a"mutually conditional presence" Active Record validation. I want that (A) if one at least is present, then the other 3 must be present (B) still allow none to be present (if the other 3 are blank, then the fourth has the right be be blank) Here are the four attributes: address_line_1 zipcode state country It means that if the user fills ONE of them then ALL the others have to be present. But

Rails 4 active record validation - conditionally validate presence of 4 attributes if at least one is present while allowing none to be present

断了今生、忘了曾经 提交于 2020-01-16 18:07:07
问题 I have a form with 10 attributes. Among them I have 4 attributes which I need to apply what I'd call a"mutually conditional presence" Active Record validation. I want that (A) if one at least is present, then the other 3 must be present (B) still allow none to be present (if the other 3 are blank, then the fourth has the right be be blank) Here are the four attributes: address_line_1 zipcode state country It means that if the user fills ONE of them then ALL the others have to be present. But

Selecting only one row from child model based upon the parent model

旧街凉风 提交于 2020-01-16 09:18:20
问题 Following is the association between 2 models: class FotoGossip < ActiveRecord::Base has_many :uploads end class Upload < ActiveRecord::Base belongs_to :foto_gossip end @latest_uploads = Upload.all(:include => :foto_gossip, :order => "created_at DESC", :limit => 5) It displays the latest 5 photos from Upload model. But, I want to display 5 images from Uploads, order_by created_date DESC but only 1 image per FotoGossip. Its something like grouping the recent FotoGossip with its one photo from