activerecord

Codeigniter - passing multiple values to view

流过昼夜 提交于 2019-12-24 04:01:30
问题 Okay, so generally I wouldn't have a problem doing this and it would be fairly straight forward, but not so much this time. Here is the relevant code in my controller: // In order to properly build the form url, we need to include the // category and product to the view $this->data = array( 'category' => $category, 'product' => $product ); // Load the product model and get editable values from the database $this->load->model('productadmin/products_model'); $productInformation = $this-

Group activerecord relation

家住魔仙堡 提交于 2019-12-24 03:59:28
问题 After a select on a table I get a result: [#<Result url: "http://www.url.com/1", key_id: 1>, #<Result url: "http://www.url.com/1", key_id: 2> [...] How can I group the url and keep the key_id , something like: ["http://www.url.com/1", [1, 2]]=>2 Thank you 回答1: Here's the code: grouped_results = Result.all.group_by(&:url).map do |url, results| { [url, results.map(&:key_id)] => results.count } end You need to replace Result.all by your actual scope or something. 来源: https://stackoverflow.com

rails: editing serialized data in form

匆匆过客 提交于 2019-12-24 03:22:45
问题 I have a model that has one param that is a serialized Hash. I want to be able to edit the hash in a form using built in rails features. It is almost working. If you fill in the form and submit it, the params are correctly serialized and in the controller the model is built with the expected values. Where it falls apart is when I want the form to display an existing model: none of the values are displayed in the form fields. My model looks like: class Search < ActiveRecord::Base serialize

How to model the concept of “Featuring” (i.e., when an artist is “featured” on a song)

谁都会走 提交于 2019-12-24 02:59:27
问题 Sometimes more than one artist will be on a song. For example, Jay-z's new song "A Star is Born" features the artist Cole, and thus would be listed as "Jay-z (featuring Cole) - A Star is Born" in a catalog. My question is how to model this in my database. Right now it's pretty simple: every song belongs_to :artist and every artist has_many :songs . I want to change this so that songs have many artists, with one artist specified as "primary" -- i.e., if A, B, and C are all associated with a

SQLite3::ConstraintException: xxxxx.created_at may not be NULL

跟風遠走 提交于 2019-12-24 02:56:25
问题 I have a resque job that creates an artist and associates it with a user. User has_and_belongs_to_many :artists, and Artist has_and_belongs_to_many :users. def self.perform(itunes_id, user_id=nil) artist = Artist.find_by_itunes_id(itunes_id) || lookup_and_create_artist(itunes_id) if user_id && user = User.find(user_id) user.artists << artist user.save! end end user.artists << artist raises this exception: ActiveRecord::StatementInvalid: SQLite3::ConstraintException: artists_users.created_at

Rails Find_by_id raises exception

余生颓废 提交于 2019-12-24 02:25:08
问题 I was led to believe that the difference between Object.find and Object.find_by_id is that find will raise a RecordNotFound exception whereas find_by_id simply returns nil if nothing is found. However, in my Rails 3 app if I attempt to search my Uploads model with a bogus id I get: ActiveRecord::RecordNotFound in UploadsController#show Couldn't find Upload with id=59 Request Parameters: {"id"=>"59"} Here is the line of code thats messing up: @upload = Upload.find_by_id(params[:id]) I'm using

Rails joins query

眉间皱痕 提交于 2019-12-24 02:14:35
问题 I have three models Tag => :id , :name Tagging => :id , :tag_id , :post_id Post => :id , :summary I know the id of the tag. I would like to query for all of the posts that have a specific tag_id , through the Taggings model. Something like @post = Post.joins(:taggings).where(:tag_id => 17) but this doesn't work because it is looking for the tag_id in the Post model and not the Tagging model. I'm not sure how to do this. 回答1: Using the .where format you can pass a string like .where("taggings

Rails 4 order by virtual attribute

泄露秘密 提交于 2019-12-24 01:49:11
问题 I have a Product model which has name and description columns in the database. I also have a Product.search_results_for(query) , where query is a string like "Green Apple" . I need to return an ActiveRecord::Relation of the results ordered by which is the best hit. Currently, I'm setting a search_result_value to each product. search_result_value IS NOT a column in the database, and I don't want it to be. So in essence, I have an ActiveRecord::Relation of Products that I need to order by

Show last message in a message array

╄→гoц情女王★ 提交于 2019-12-24 01:44:57
问题 Regarding my last attempted answer, I could achieve what I wanted. Once the app is working I noticed something is different: only the person that created the message and sends a message was only showing. If I reply, I do not see the reply and I know the problem. My Message model has tables: [:id, :user_id, :to, :body, ...] :user_id would be similar to from as that is used to determine who created the message. User 1 would be Client and User 2 is Student. Student sends a message to Client.

Ransack search on data stored in array (operator does not exist: integer[] = integer)

可紊 提交于 2019-12-24 01:39:26
问题 I have a Rails model call InfoData and it has an attribute called error_codes. The codes are stored in an array like so [9,7,10,21] (integer[]) . To recap InfoData.first.error_codes => [9,7,5] I try to use ransack on it in order to search if a specific code is present(via a select option). For error_codes_in (_in ransack predicate) I receive the following error operator does not exist: integer[] = integer LINE 1: ...ranch_id" WHERE "info_data"."error_codes" IN (9) A... ^ HINT: No operator