activerecord

ActiveRecord query, order by association, last of has_many

天大地大妈咪最大 提交于 2019-12-07 13:55:11
问题 Im trying to list all Users by the created_at column of the most recently created associated recored (communications). What I have so far: User.includes(:communications).order( 'communications.created_at IS NULL, communications.created_at asc' ) As it is, desc works as I expect. The issue is when the order is reversed and I try order asc . It appears that is's because the user can have many communications , The query returns the list of users in order of the first communications created

Flag invalid attribute in ActiveRecord

泄露秘密 提交于 2019-12-07 13:22:57
问题 I am trying to implement functionality wherein an attribute, once set, cannot be changed on an ActiveRecord model. To this end, I have written the following methods: def address self[:address] end def address=(val) if new_record? self[:address] = val else errors.add(:address, "Cannot change address, once it is set") return false # tried return nil here first, did not work end end Am I doing something wrong here? I want the object to be invalid once I try to change an address, but I do not get

ActiveRecord serialize not working properly with Hash column

穿精又带淫゛_ 提交于 2019-12-07 13:21:33
问题 I'm trying to store a Hash in a table column, using ActiveRecord's serialize method but I can't make it work. I'm using Rails 4.2.0 and RailsApi 0.3.1 This is my model: class Agreement < ActiveRecord::Base serialize :phone_numbers, Hash end phone_numbers is a text column like it's required. Then in the console: a = Agreement.new(phone_numbers: {"dario" => "12345"}) a.phone_numbers => "{\"dario\"=>\"12345\"}" #(Note this is a string, not a Hash as I would expect) a.phone_numbers["dario"] =>

How do you concatenate two active record results to return a new result that can further be filtered?

大憨熊 提交于 2019-12-07 13:15:37
问题 Imagine the scenario... #models/user.rb class User < ActiveRecord::Base has_many :accounts, :conditions => { :active => 1 } end #models/account.rb class Account < ActiveRecord::Base belongs_to :user def public_accounts Account.all :conditions => { public => true } end end Now imagine I want to concatenate User(:id).accounts with Account.public_accounts to show a list of all accounts available to a user. So you'd think I'd be able to update the User model to look like this. #models/user.rb

DATE_FORMAT within a query in CodeIgniter using Active Record don't work

大城市里の小女人 提交于 2019-12-07 12:41:09
问题 coders. I'm running a little problem here and can't find the solution. I'm building a query using Active Record from CI. This is the code for the query: $this->db->select("u.id AS user_id, u.email, p.display_name, p.first_name, p.last_name, s.status_id, s.message"); $this->db->select("DATE_FORMAT(s.created_at, `Publicado el %d/%m/%Y a las %h:%i %p`) AS created_at", FALSE); $this->db->join($this->_table_users . ' u', 'u.id = s.user_id'); $this->db->join($this->_table_profiles . ' p', 'p.user

Ruby On Rails multiple composite primary keys question

浪子不回头ぞ 提交于 2019-12-07 11:19:29
I am a new guy in Ruby, and I have tables with these primary keys: transaction_types: transaction_type transaction_headers: transaction_type transaction_year transaction_id transaction_details: transaction_type transaction_year transaction_id city_id ticker_id tickers: city_id ticker_id Of course, those models have other non primary keys such as customer_id, connection_id or date, or user_id, etc, but those are not important for relationships, as those are merely data or I don't have any problem with those. These are my models: #models class transaction_type < ActiveRecord::Base has_many

Can somebody give an active record example for pluginaweek - statemachine?

自作多情 提交于 2019-12-07 11:14:51
问题 Can somebody give a simple example on howto use pluginaweek state_machine for a ticket model with active record? I do not understand the complex examples from the docs. Example states: new -> accepted, declined, feedback accepted -> solved or feedback feedback -> accepted or solved 回答1: Example ticket model (not tested) class Ticket < ActiveRecord::Base attr_accessible :name, :description attr_accessible :state_event validates :name, :presence => true state_machine :initial => :new do event

Chaining multiple joins with wheres in rails3

扶醉桌前 提交于 2019-12-07 11:13:06
问题 Trying to construct a query such that I have multiple statement specifying joins, each with a where message chained onto them. When the query is run, I get all the joins, but only the where from my first call. Here's the method body that's doing the query: observations_joins = Observation.joins(:obs_session => :project).where(:obs_sessions=>{:project_id=>self.project.id}) descriptor_hash = descriptor_where_hash if tag_descriptors && tag_descriptors.size > 0 puts "The descriptor_hash: #

Using check boxes with a has_many relationship

a 夏天 提交于 2019-12-07 10:57:06
问题 I have an invoice with some lines. A line can only belong to one invoice. This is how my schema looks like: create_table "invoices" do |t| end create_table "lines" do |t| t.integer "invoice_id" end And my models: class Invoice < ActiveRecord::Base has_many :lines end class Line < ActiveRecord::Base belongs_to :invoice end Now, when creating (or editing) an invoice I would like to show a list with all possible lines (the lines already exist in the database) and have a check box for each line

Rails 3.0.10 before_validation callback not called for associated collection objects

喜欢而已 提交于 2019-12-07 09:38:27
问题 I've got an object called a Parent that has_many Child objects: has_many :children accepts_nested_attributes_for :children, :allow_destroy => true Child includes a Module that specifies a :before_validation callback: def self.included base base.class_eval do before_validation :my_callback end end protected def my_callback logger.debug "see me!" end I've noticed that when creating a Parent and nesting attributes for children, the :before_validation callback is not being invoked for each Child.