activerecord

Representing ecommerce products and variations cleanly in the database

倖福魔咒の 提交于 2019-12-17 21:56:55
问题 I have an ecommerce store that I am building. I am using Rails/ActiveRecord, but that really isn't necessary to answer this question (however, if you are familiar with those things, please feel free to answer in terms of Rails/AR). One of the store's requirements is that it needs to represent two types of products: Simple products - these are products that just have one option, such as a band's CD. It has a basic price, and quantity. Products with variation - these are products that have

Model.reset_column_information does not reload columns in rails migration

二次信任 提交于 2019-12-17 21:56:16
问题 I'm using Rails 3.2 and have a migration that contains the code: add_column :users, :gift_aid, :integer, :default => 2 # reset columns User.reset_column_information ... code here to load legacy data from sqlite3 database ... # now create a user with the loaded column data user = User.create( ...other cols..., :gift_aid => migrated_gift_aid_column_data, ...other cols... ) and I get unknown attribute: gift_aid when running the migration. User.column_names shows the same list before and after

codeigniter : Getting data posted in between two dates

*爱你&永不变心* 提交于 2019-12-17 21:55:12
问题 How can I retrieve data from the database by querying records between two dates using codeigniter's activerecord? thanks 回答1: This looks like what you need: $this->db->where('order_date >=', $first_date); $this->db->where('order_date <=', $second_date); return $this->db->get('orders'); 回答2: Try This: $this->db->where('sell_date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"'); Hope this will work 回答3: This worked great for me $this->db-

How to create a full Audit log in Rails for every table?

混江龙づ霸主 提交于 2019-12-17 21:52:48
问题 We recently began a compliance push at our company and are required to keep a full history of changes to our data which is currently managed in a Rails application. We've been given the OK to simply push something descriptive for every action to a log file, which is a fairly unobtrusive way to go. My inclination is to do something like this in ApplicationController : around_filter :set_logger_username def set_logger_username Thread.current["username"] = current_user.login || "guest" yield

Rails Search with optional parameters?

拜拜、爱过 提交于 2019-12-17 21:49:40
问题 How would I do a search on a database when the search can provide many optional parameters such as ID, Zip, City, and State? These can either have values or be blank entirely. How would I make a rails query like that? 回答1: The usual advice is to move logic to the model and keep the controller as lean as possible. There are different approaches for the filter method, the first one: class Record < ActiveRecord::Base def self.filter(attributes) attributes.select { |k, v| v.present? }.reduce(all)

I need help properly forming this ActiveRecord association

二次信任 提交于 2019-12-17 21:37:08
问题 Basically, a User can participate in one or more events as either (or potentially both) a vendor and as a member of the event's faculty. A user is, by default, categorized as neither a vendor nor a faculty member but rather attains either (or both) status(es) within the context of an event (e.g., the user has been admitted to an event as a member of its faculty). It seems like what I want to say is that the user has many events through either (or both) the vendors or the faculty linking

Yii - echo the last query

不羁的心 提交于 2019-12-17 20:14:16
问题 I have used Codeigniter before and it has a feature that will echo the last database query - really useful for debugging. eg... $this->getSomeData->findAll(); Yii command to output this single query e.g SELECT * FROM TABLE... Does Yii have a similar feature to simply show the very last query executed? 回答1: Try this in your config file. You can see the query and other details at the bottom of the page. 'db'=>array( 'enableProfiling'=>true, 'enableParamLogging' => true, ), 'log'=>array( 'class'

Rails reports can't find a column that is there

有些话、适合烂在心里 提交于 2019-12-17 19:59:30
问题 I am currently trying to do a complicated WHERE search on a table using Rails, the trouble is I get the error: PG::Error: ERROR: column "email" does not exist LINE 1: SELECT "bans".* FROM "bans" WHERE (Email='' AND IP='' AND (... ^ : SELECT "bans".* FROM "bans" WHERE (Email='' AND IP='' AND (Username='NULL' )) And I know that column actually exists, and doing a rails dbconsole gives me the following: Jungle=> select * from bans; id | Username | IP | Email | Reason | Length | created_at |

How to use `to_sql` in AREL when using `average()`?

强颜欢笑 提交于 2019-12-17 19:58:28
问题 I am trying to the get SQL from AREL, but it does not work in case I use average(:stars) : This works: Review.where("reviewed_user_id = ?", self.reviewed_user_id).to_sql #=> "SELECT `reviews`.* FROM `reviews` WHERE (reviewed_user_id = 3)" This causes NoMethodError : Review.where("reviewed_user_id = ?", self.reviewed_user_id).average(:stars).to_sql #=> undefined method `to_sql' for 3:Fixnum So that means that to_sql is getting called on the result of the AREL instead of on the AREL object -

ActiveRecord Find - Skipping Records or Getting Every Nth Record

倖福魔咒の 提交于 2019-12-17 19:47:47
问题 I'd like to do a query where I select a bunch of data, but I'd like to be able to then decrease the resolution of that data by only selecting, say, every third record, or maybe even every hundredth record, or whatever. Is there any straightforward way to do this with ActiveRecord? 回答1: In Oracle i would write that as follows: YourModel.find(:conditions => 'MOD(ROWNUM,3) = 0') this has the advantage that the filter happens at the database, so not everything is retrieved. In PostgreSQL this is