activerecord

Rails Filter records of child model based upon the parent model attribute

你离开我真会死。 提交于 2020-01-01 19:47:27
问题 Following are the 1-to-M models: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, ... end class Upload < ActiveRecord::Base belongs_to :foto_gossip end Now I want the Uploads.all with the condition :published_at NOT NULL of the corresponding upload's parent model? 回答1: Just add this to your Upload model: named_scope :with_published_foto_gossip, :joins => :foto_gossip, :conditions => "foto_gossips.published_at IS NOT NULL" then you can get all the uploads

Getting OCIError: ORA-00932: inconsistent datatypes From Rails 3

孤人 提交于 2020-01-01 19:00:51
问题 Here is the scenario. I'm writing my geo-ruby oracle adapter for Ruby On Rails which supports working with SDO_GEOMETRY out of box. It was going well. I wrote codes for selecting SDO_GEOMETRY objects from Oracle DB successfully. Everything ruins up when I wanted to write insert and update parts. Following is what's in my mind. I want to be able to do this statement: g = GeoShape.new(name:"point1", shape: Point.from_x_y(-34,-43,4326)) g.save I generated following sql query from the above

ActiveModel::SecurePassword undefined method `password_digest='

馋奶兔 提交于 2020-01-01 18:58:24
问题 I try to use rails 3.1 ActiveModel::SecurePassword by following http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword and I end up with red light ... user.rb class User < ActiveRecord::Base has_secure_password validates :password, :presence => { :on => :create } end factory.rb Factory.define :user do |f| f.email "foo@bar.com" f.password "foobar" f.password_confirmation { |u| u.password } end spec_user.rb describe User do it "should authenticate with matching

ActiveModel::SecurePassword undefined method `password_digest='

戏子无情 提交于 2020-01-01 18:55:09
问题 I try to use rails 3.1 ActiveModel::SecurePassword by following http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword and I end up with red light ... user.rb class User < ActiveRecord::Base has_secure_password validates :password, :presence => { :on => :create } end factory.rb Factory.define :user do |f| f.email "foo@bar.com" f.password "foobar" f.password_confirmation { |u| u.password } end spec_user.rb describe User do it "should authenticate with matching

Rails: Need help defining association for address table

强颜欢笑 提交于 2020-01-01 17:26:00
问题 I finding this a bit mind bending and would like some ideas. My design goes likes this: There is builders table, each builder can have a postal address There is a clients table, each client can have a postal address and a billing address There is an addresses table I need to define the associations between the builders-addresses and clients-addresses My initial database design was like this: Builders -------------------- id postal_address_id ... Clients -------------------- id postal_address

Dynamic ActiveRecord finder methods chaining based on params

[亡魂溺海] 提交于 2020-01-01 14:35:21
问题 Im new to rails and I want to make a select query based on diffrent GET params (filtering and sorting). Can I some how add conditions to find in the code? For example: if params[:ratings] Movie.where(:rating => params[:ratings].keys) end and then add ordering and other where conditions. How can I achive this? Maybe there is a better way to dybamicly modify select query (without making SQL string). Thanks. 回答1: The where , order , ... methods return ActiveRecord::Relation objects so you can

Rails 3 ActiveRecord Transactions

喜欢而已 提交于 2020-01-01 12:05:36
问题 I have a model method that I'd like to call from various controllers. It looks something like this: def Post < ActiveRecord::Base def read! self.read_at = Time.now self.save self.thread.status = Status.find_by_name("read") self.thread.save end end In my controller, if I call @post.read! , will this rollback on any errors? 回答1: In your current setup, if read_at gives an error, it will still continue onto the code that executes thread.status for example. You want to use ActiveRecord

Rails 3 ActiveRecord chaining

﹥>﹥吖頭↗ 提交于 2020-01-01 11:57:35
问题 I'm new to Rails and I'm not sure why my chaining isn't working. Working my_model.select('name').where('status_id = 6').all Not Working my_model.select('name').where('status_id = 6').order('name') Why does chaining the order after the where not execute the query? I've tried adding the .all after the .order but that didn't seem to work either. 回答1: Looks like this is a known bug in the jdbc adapter: http://kenai.com/jira/browse/ACTIVERECORD_JDBC-154 If you make these changes it will fix it:

Find records that were created closest to the current date

纵饮孤独 提交于 2020-01-01 10:13:27
问题 I would like to get the records that have their created_at date closest to the current date. How can I do this with active records' where clause? 回答1: You could find the closest record in the past with something like: Record.where("created_at <= ?", Date.today).order_by("created_at DESC").limit(1) Similarly, you can have the closest record in the future Record.where("created_at >= ?", Date.today).order_by("created_at ASC").limit(1) And then compare wich one is the closest to current date...

how to build query like `select … where id % 2 = 1` using Yii 2 ?

流过昼夜 提交于 2020-01-01 10:10:06
问题 Surely, I can Yii::$app->db->createCommand($sql)->query() , but what if I wanna use a ActiveRecord::find()->where($conditions) to do this job ? 回答1: Here is one of the options with using yii\db\Expression : use yii\db\Expression; ... $models = Customer::find() ->select(['id', 'name', ...]) ->where(new Expression('id % 2 = 1')]) ->all(); It's definetely better than raw sql and ['%2=', 'id', 1] because it follows the order and more readable in my opinion. ['%2=', 'id', 1] also is not suitable