activerecord

Date reverts to 2000-01-01 from ActiveRecord to MySQL

青春壹個敷衍的年華 提交于 2019-12-08 06:05:27
问题 I am trying to save a time in a MySQL table. However, the date reverts to 2000-01-01. 1.9.2p320 :036 > vv = Visitor.new => #<Visitor id: nil, ip_address: nil, num_day_visits: nil, last_visit: nil> 1.9.2p320 :037 > vv.last_visit = Time.now; vv.ip_address = "3.3.3.3" => "3.3.3.3" 1.9.2p320 :038 > vv.num_day_visits = 1 => 1 1.9.2p320 :039 > vv => #<Visitor id: nil, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2012-10-11 01:31:04"> 1.9.2p320 :040 > vv.save SQL (0.2ms) BEGIN SQL (0.7ms)

ActiveRecord: How to interpolate attribute value to a custom validation errror message?

Deadly 提交于 2019-12-08 06:02:34
问题 I have a validates_presence_of :price validation, which returns a "Book version price can't be blank" error when the validation fails. class BookVersion < ActiveRecord::Base attr_accessible :name, :book_id, :instock, :isbn, :price, :famis_price, :famis_number, :weight_in_pounds, :width, :height, :thickness validates_presence_of :price, message: "can't be blank" However, I want to add the name value of the BookVersion to the validation message like so: If book_version.name returns "LB": Book

How do I turn off tinyint boolean emulation in Rails 3.2.2?

爷,独闯天下 提交于 2019-12-08 05:47:54
问题 As per the api docs: If you wish to disable this emulation (which was the default behavior in versions 0.13.1 and earlier) you can add the following line to your application.rb file: ActiveRecord::ConnectionAdapters::Mysql[2]Adapter.emulate_booleans = false But when I do so, I get: uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter 回答1: For someone see this topic later, I needed to require mysql2_adapter with full path in Rails 3.2.0 ; module MyApp class Application <

How to Generate Castle ActiveRecord C# Classes for an Existing Database

佐手、 提交于 2019-12-08 05:33:25
What is the best way to generate C# classes for use with Castle ActiveRecord and NHibernate given an existing database structure? Can you recommend any of the class generation tools or is it just less hassle to write the classes by hand? Michael Maddox If you only have a handful of tables with not many columns, it may be best to write your classes by hand. I wouldn't recommend hand writing many classes though if your database already exists. Active Writer may meet your needs: http://using.castleproject.org/display/Contrib/ActiveWriter There are other code generators in various states of

Accessing array of parameters inside the params[] value

江枫思渺然 提交于 2019-12-08 05:27:18
问题 I have the following Parameters: Parameters: {"utf8"=>"✓", "authenticity_token"=>"06Us9R1wCPCJsD06TN7KIV/2ZeH4dJlZVqc12gpKBbo=", "run"=>{"box_id"=>"1"}, "tapes"=>{"1"=>{"tape_id"=>"1"}, "2"=>{"tape_id"=>"2"}, "3"=>{"tape_id"=>"3"}}, "commit"=>"Create Run"}} I want to create a new "Run" with the box id of 1 and then associate the tapes 1 2 and 3 to this run with the box id of 1 I am not sure what code needs to go into the controller, i did try: def create @run = Run.new(params[:run]) @tape_ids

Rails 3 Polymorphic Association between one MongoMapper model and one/many Active Record model/s

為{幸葍}努か 提交于 2019-12-08 05:26:56
问题 I have a Record model (Active Record) that stores some custom logs. Record is in polymorphic association with all the other model in my app, and I can effectively log what I want hooking my Record methods in the other controllers. What I need: To have the logs in a separate database. So I have to: Be able to manage two different databases in my apllication (one is Postgres/ActiveRecord and the other one is MongoDB/MongoMapper) Generate a polymorphic association between my Record model, now

Ruby on Rails : how to generate an application-wide sequence?

Deadly 提交于 2019-12-08 05:26:49
问题 I am developing on RoR 4, with Oracle, PostGreSQL and MSSQL as target databases. I am building a hierarchy of 4 objects, for which I need to display parent-child relationships through the same query whatever level I start from. Not easy to figure out, but the hint is that none of the object should have identical IDs . The issue here is that rails maintains a dedicated sequence for each object, so duplicated IDs will appear for sure. How can I create a sequence to fill a unique_id field which

Complex associations in ActiveRecord models

删除回忆录丶 提交于 2019-12-08 04:50:49
问题 I'm trying to understand how ActiveRecord deals with associations that are more complex than simple has_many , belongs_to , and so on. As an example, consider an application for recording music gigs. Each Gig has a Band, which has a Genre. Each Gig also has a Venue, which has a Region. In the rough notation of MS Access (which I'm suddenly beginning to feel quite nostalgic for) these relationships would be presented like this 1 ∞ 1 ∞ ∞ 1 ∞ 1 Genre ---- Band ---- Gig ---- Venue ---- Region I

Should I include other fields in a HABTM table?

让人想犯罪 __ 提交于 2019-12-08 04:48:47
问题 I would like an Order object to be comprised of many Product objects, so I set up a HABTM relationship on object. I'm wondering if it's "correct" (or the Ruby/Rails) way to also include additional data within the HABTM table. For instance, if I need to compute the subtotal and there's a chance the line-item totals might need to be overridden, do I store that as part of the association table, or do I need a LineItem object or something better? Thanks ActiveRecord::Schema.define(version: 3) do

How to eager load a polymorphic model?

瘦欲@ 提交于 2019-12-08 04:43:27
问题 I'm trying to optimize some of our queries. One particular challenge was trying to eager load a polymorphic model. In this case, UserView is polymorphic and has the following columns: user_id, user_viewable_id, user_viewable_type When I try to run this query. @user_views = UserView.select('user_views.* AS user_views, songs.* AS songs, users.* AS users') .joins('INNER JOIN users AS users ON user_views.user_id = users.id') .joins('INNER JOIN songs AS songs ON user_views.user_viewable_id = songs