activerecord

ActiveRecord relationships for a join table linking two records of the same table?

让人想犯罪 __ 提交于 2019-12-11 13:45:59
问题 I have a Character model and a Link model. The Link model represents a link from a character to another character. A link has a textual 'description' attribute. A link from character A to character B is distinct from the opposite link from B to A. A character has zero or one link to another character. A character may have various links to different characters. A character may be linked to by various different characters. I used used Active Record relationships to partly implement

Ruby on Rails optimal search with array of filter values?

回眸只為那壹抹淺笑 提交于 2019-12-11 13:41:01
问题 I have a specific example, but the general question is: whats the optimal way to retrieve records when you have an array of filter values to match? Say I have User and Post records where User has_many :posts and I have a Relationship model that looks like this: class Relationship < ActiveRecord::Base belongs_to :follower, class_name: "User" belongs_to :followed, class_name: "User" validates :follower_id, presence: true validates :followed_id, presence: true end I want to write a function that

ActiveResource + Caching

馋奶兔 提交于 2019-12-11 13:36:50
问题 I'm building an application that consumes models through an api with ActiveResource. I noticed that the @resource ||= @resource.do a query doesn't work, i.e. If I put something like that in my controller, my application will still query the api. So there is no built in caching that I'm used to with ActiveRecord. Time to expand my knowledge and skill base, ok. I found this: http://injectisforwizards.com/blog/read-through-caching-of-activeresource/, and while I don't understand this 100% yet,

Is it possible to trick ActiveRecord into understanding it has all the data it needs to create related models in memory?

巧了我就是萌 提交于 2019-12-11 13:27:20
问题 For a couple of years, I've worked to make a database that holds the data for an incredibly-complicated engineering process. Right now, it's served by an SQL Server database in Azure, with a WinForms (w/ DevExpress) client, and an Azure worker role for doing a couple of long-running processes which center on our main file format, and a library which operates on it. I've written a data-access library that's essentially a really crappy ORM. I'm tired of this architecture. All along, I've

Ruby Workflow Issue During Migration

浪尽此生 提交于 2019-12-11 13:23:17
问题 I am using Ruby Workflow in my ActiveRecords using Gem: Workflow Existing Running Code contains: I am having an ActiveRecord: X I am having two Migrations already: (Ref1) CreateX migration (which creates table X) (Ref2) CreateInitialEntryInX migration (which creates one entry in table X) New Changes: Now I wanted to add workflow in ActiveRecord X, hence I did: (Ref3) I added the workflow code in ActiveRecord Model X (mentioning :status as my workflow field) (Ref4) AddStatusFieldToX migration

Making ActiveRecord join model attributes available in query results

别说谁变了你拦得住时间么 提交于 2019-12-11 12:49:30
问题 Given User and Book models, I've created a join model, ViewedBook, that contains additional attributes. Below is the essence of what I've come up with: create_table "users" t.string "username" end create_table "books" t.string "title" t.integer "user_id" t.date "authored_date" end create_table "books_viewings" t.integer "book_id" t.integer "user_id" t.boolean "finished" t.date "last_viewed_date" end class User belongs_to :book_viewing has_many :authored_books, :class_name => "Book", :source =

Defining table name for has_many self joins in rails?

两盒软妹~` 提交于 2019-12-11 12:41:47
问题 I wish to set up nested comments, and want to use self-join to set this up. class Comment < ActiveRecord::Base has_many :children, :class_name => 'Comment' #... end Now, what sql table structure would I use to set up the has_many self-join? I'm assuming something like this: comment_to_comments: parent_id integer child_id integer How do I tell rails to use this table? How do I tell rails that parent_id is the foreign key to reach the parent and the child_id is the foreign key to reach the

createdb not recognized as a command when using ActiveRecord

跟風遠走 提交于 2019-12-11 12:39:42
问题 I'm a newbie to Ruby and web development. I'm using Windows 7 64-bit with Ruby 2.0 and I have PostgreSQL 9.4 installed. I'm trying to use ActiveRecord to create a database. I checked that my postgresql server is running and I did bundle install to make sure I had all the required gems. However, when I try to do the terminal command "bundle exec rake create:db" it tells me that "'createdb' is not recognized as an internal or external command, operable program or batch file." I also did the

ActiveRecord doesn't work on one table

不想你离开。 提交于 2019-12-11 12:32:23
问题 I have a Rails Model: ruby-1.9.2-p0 > NavItem => NavItem(id: integer, item_identifier: string, description: string, description2: string, packing_unit: string, sales_unit_of_measure: string, ean_code: string, evp_price: string, item_category_code: string, class: string, product_group_code: string, maintenance_status: string) If I want to create a record: ruby-1.9.2-p0 > NavItem.create NoMethodError: undefined method `has_key?' for nil:NilClass from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems

Converting SQL query to Codeigniter Active Record

天涯浪子 提交于 2019-12-11 12:14:52
问题 I have this sql query SELECT * FROM adm_species t JOIN adm_breeds e ON e.species_id = t.id JOIN fd_registrations s ON s.breeds_id = e.id WHERE t.code = 'cat' AND s.sex_id = '2' AND s.ownername LIKE '%sha%' How do I convert this to Codeigniter Active Record? Thanks in advance! 回答1: Try this $this->db->select('*'); $this->db->from('adm_species t'); $this->db->join('adm_breeds e', 'e.species_id = t.id'); $this->db->join('fd_registrations s', 's.breeds_id = e.id'); $this->db->where('t.code', 'cat