activerecord

Relations function in AR Model, many to one relationship

大兔子大兔子 提交于 2019-12-08 03:31:01
问题 So here's the scenario: I have two table, Issue & Project. A Project can have many Issue and an Issue can exactly one project. Since Issue is many to one, do you have to define it? Cause I know in Project Model I have: public function relations() { return array( 'issues' => array(self::HAS_MANY, 'Issue', 'project_id'), 'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'), ); } For Issue Model I have nothing but foreign keys: public function relations()

CodeIgniter ActiveRecord join() method wrapping numbers with backticks

让人想犯罪 __ 提交于 2019-12-08 03:25:37
问题 This is my active record code in CodeIgniter: $this->db->... $this->db->join('post_likes', 'post_likes.user_id="'.$this->db->escape($online_user).'" AND post_likes.post_id=post.id', 'left'); And this is how it is interpreted: LEFT JOIN `post_likes` ON `post_likes`.`user_id`="`3"` AND post_likes.post_id=post.id it gives the error: `user_id`="`3"` How to write a direct number in active record? Update: removing escape to test it on your computer you dont need to have a database. Just trying this

Rails: Implementing a reuseable Comment model

岁酱吖の 提交于 2019-12-08 03:20:34
问题 I have a Comments model, and I also have a Video, and Photo model. Now, I want for my Video and Photo models to have_many comments, but that means my Comment model will have to have a belongs to :video and a belongs_to :model (as well as foreign keys for each model in the database). Now say I create a Post model in that same application and I want it to have many comments, that would mean I would have to add belongs_to :post to my Comment class. In rails is there a better way to implement a

Inserting Multiple Records at a time into Model [duplicate]

北城以北 提交于 2019-12-08 03:17:08
问题 This question already has answers here : How to implement bulk insert in Rails 3 (4 answers) Closed 6 years ago . I am looking for some best practices when it comes to inserting data into a model, especially when there are a lot of records to be created, so far I am retrieving some XML and saving it into a model doc = Nokogiri::XML.parse(open(url)) doc.xpath('//xmlns:feed/xmlns:entry[xmlns:title[node()]]').each do |s| cid = s.xpath("xmlns:id").text email = s.xpath("gd:email/@address").text

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

故事扮演 提交于 2019-12-08 03:01:25
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 remains unique for all my data ? Thanks for your help, Best regards, Fred I finally found this solution

Rails: run rake tasks like migrations

大兔子大兔子 提交于 2019-12-08 03:00:32
问题 My dev team needs to more precisely run rake tasks. There are certain tasks that need to be only run once after a specific code change. Without getting too specific, it would be like needing to update certain existing users records after a new business rule for new users is implemented in the code. We like how migrations use a db table for logging. Is there a similar tool for rake tasks? Can we hack Rails'/ActiveRecord's migrations system for rake tasks? We'd prefer not to mix db-related

Creating dropdown list from related table yii

半世苍凉 提交于 2019-12-08 02:43:34
问题 I have two tables: product and product_type (related to the models resp. Product and Product_type): product : product_id, product_type_id, name, etc... product_type : product_type_id, name, desc, etc... Both are related with key "product_type_id". I have created the crud for both tables using gii crud generator. Now on Product form page I want to display the listing of all product_type name in dropdown field using Yii ActiveRecord. I have added this line in views/product/_form.php script: <

best_in_place with a many to many relationship in rails 4

你。 提交于 2019-12-08 02:34:24
问题 I have made a github repo that you can find here just for this question. I have 3 models: class User < ActiveRecord::Base has_many :user_countries has_many :event_countries, -> { where(user_countries: {:event => true}) }, :through => :user_countries, :source => :country has_many :research_countries, -> { where(user_countries: {:research => true}) }, :through => :user_countries :source => :country end class UserCountry < ActiveRecord::Base belongs_to :country belongs_to :user end class Country

ORDER BY columns that are sometimes empty using Active Record & Rails

橙三吉。 提交于 2019-12-08 02:08:07
问题 In my rails app (using postgresql), I'm trying to compose an Active Record query to find a group of volunteer records and then order them by first_name , then last_name , then email . Additionally, first_name and last_name may be null (either both will be null or both will be not null ). For example, I would want to following list to be sorted thusly: Volunteer [first_name: 'Alex', last_name: 'Diego', email: 'a.diego@person.com'] Volunteer [first_name: null , last_name: null , email: 'cxxr

Rails ActiveRecord friendly code from a Complex Join, Sum, and Group query

我与影子孤独终老i 提交于 2019-12-08 01:44:50
问题 PROBLEM Hello, I am having no luck trying to break down this SQL statement into ActiveRecord/Rails friendly code and I'd like to learn how I can avoid a find_by_sql statement in this situation. Scenario I have users that create audits when they perform an action. Each audit is of a specific audit_activity. Each audit_activity is worth a certain number of points, based on score_weight. I need to find the total scores of each user, based on their total accumulated audit_activity score_weights.