activerecord

Dynamic class_name for has_many relations

对着背影说爱祢 提交于 2019-12-10 13:04:35
问题 I'm trying to make has_many relation with dynamic class_name attribute class Category < ActiveRecord::Base has_many :ads, :class_name => ( lambda { return self.item_type } ) end or class Category < ActiveRecord::Base has_many :ads, :class_name => self.item_type end But i got errors: can't convert Proc into String or undefined method `item_type' for #<Class:0xb62c6c88> EDIT I have two different types of Ads LeaseAd , RentAd they implemented using single table inheritance Then i have Category

Reusing named_scope to define another named_scope

﹥>﹥吖頭↗ 提交于 2019-12-10 13:03:54
问题 The problem essence as I see it One day, if I'm not mistaken, I have seen an example of reusing a named_scope to define another named_scope. Something like this (can't remember the exact syntax, but that's exactly my question): named_scope :billable, :conditions => ... named_scope :billable_by_tom, :conditions => { :billable => true, :user => User.find_by_name('Tom') } The question is: what is the exact syntax, if it's possible at all? I can't find it back, and Google was of no help either.

Rails after_save callback being called multiple times

帅比萌擦擦* 提交于 2019-12-10 12:53:42
问题 I'm trying to inject an after_save callback via a mixin, but my rspec tests are telling me that the callback is being called twice when the create method is called. Why is the method being called twice? The following rspec test fails it 'should call callback' do Product.any_instance.should_receive(:update_linkable_attachments).once Product.create(:name=>'abc') end The failure message is: Failure/Error: Unable to find matching line from backtrace (#<Product:0xb7db738>).update_linkable

Rails 3 ActiveRecord: UNION

孤街醉人 提交于 2019-12-10 12:53:01
问题 Is there any way to use MySQL UNION in Rails 3? 回答1: I think the only way you're going to get this to work by directly executing the query. ActiveRecord::Base.connection.execute("SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10)") This returns an ActiveRecord resultset. If you want the results wrapped in a model do something like this: MyModel.find_by_sql("...") 回答2: I found a neat hack using select . For example if you want to make a union between User and OtherUser . User.select('id from

Validating boolean value in Rspec and Rails

落爺英雄遲暮 提交于 2019-12-10 12:43:17
问题 I'm pretty confused how to validate boolean values in Rspec and Rails. I understand everything except for false and nil are taken as true in Ruby. But when I use MySQL with Rails, it uses 1 for true and 0 for false (if my understanding is correct). I have the following model spec. I'd like to test boolean value for superuser attribute. How can I write specs here? How can I write implementation code here? Are my specs and implementation code specific to a specific database (like MySQL and

Reloading an object not working in rspec

穿精又带淫゛_ 提交于 2019-12-10 12:42:58
问题 I am trying to test a controller method with the following code: it "should set an approved_at date and email the campaign's client" do @campaign = Campaign.create(valid_attributes) post :approve, id: @campaign.id.to_s @campaign.reload @campaign.approved_at.should_not be(nil) end However, when I run this test, I get the following error: Failure/Error: @campaign.reload ActiveRecord::RecordNotFound: Couldn't find Campaign without an ID When I run the analagous lines in the rails console, the

How do you change your Rails App Data?

大兔子大兔子 提交于 2019-12-10 12:34:55
问题 I have seen a lot of talk regarding ActiveRecord Migrations and whether or not they should be used to change data within your application, some people saying yes some saying no. My question is if you are not using Migrations to do this then what are you using? Just another script that you write? I am after suggestitions on alternative ways and why they might be a better idea than just using migrations. 回答1: One problem comes if you use the provided rake db:reset and rake db:schema:load tasks,

Naming conventions for Rails migrations

六眼飞鱼酱① 提交于 2019-12-10 12:33:13
问题 Is there a best practice naming convention for Rails migrations, particularly when editing a model? e.g. if I'm adding a column bar to the Foo model, should I name it edit_foo or add_bar_to_foo I'm assuming if I'm editing mutliple models then I should create multiple migrations, but what if I'm making multiple modifications to a single model, do I name it add_bar_remove_x_edit_y_to_foo ? 回答1: I agree with the previous poster. The naming should focus on readability. But also keep in mind that

ActiveRecord includes not working with foreign keys

允我心安 提交于 2019-12-10 12:26:18
问题 I am having a hard time getting table associations to work properly. I have 2 classes (tables) that I need to associate with each other. class CaseModel < ActiveRecord::Base self.primary_key = 'seq_id' has_many :model, foreign_key: 'model_seq_id' end class Model < ActiveRecord::Base self.primary_key = 'seq_id' self.table_name = 'model' belongs_to :case_model, foreign_key: 'seq_id' scope :with_case_info, ->{includes(:case_model)} end When I run Model.with_case_info I get the following SQL:

Ruby on Rails Models. Why does a decimal{2,1} with scope 1 allow more than digit after the decimal point?

情到浓时终转凉″ 提交于 2019-12-10 12:22:47
问题 I'm having an issue with a table accepting too many digits after the decimal, despite defining it's precision and scope. rails generate model Hotel name:string 'rating:decimal{2,1}' class CreateHotels < ActiveRecord::Migration def change create_table :hotels do |t| t.string :name t.decimal :rating, precision: 2, scale: 1 t.timestamps end end end However, I am able to do the following. Hotel.create!(name: “The Holiday Inn”, rating: 3.75) Additionally, I have a rooms table (Room model), with t