Tag: mysql

将XML文件中的数据导入MySQL数据库

我想构建一个Rails应用程序,将数据从XML文件导入MySQL数据库。 有帮助吗?

Rails Arel与连接表上的where条件连接

我正在尝试将以下Rails where子句转换为使用Arel,主要是为了利用Arel提供的方法。 发布模型 class Post belongs_to :user end 用户模型 class User has_many :posts end 我正在寻找Mark发布的post。 这是Rails查询: Post.joins(:user).where(users: { first_name: ‘Mark’ }) 我需要用Arel转换这个查询。 提前致谢!

Activerecord:获取特定列和关联的关联计数

好吧,所以我们的Accounts有很多具有很多Impressions Schedules 。 我想获取Accounts.name ,所有Accounts.schedules ,但只获取Accounts.schedules.date , Accounts.schedules.summary以及Accounts.schedules.summary的数量。 这里是一个可视化的我想要给一个帐户(我想要所有帐户) Account: .name Schedules .date .summary count(Schedule.impressions) 以下是查询过多的代码 Account.find_each do |account| puts a.name # Account.name a.schedules.each do |schedule| puts schedule.date # Account.schedule.date puts schedule.summary # Account.schedule.summary puts schedule.impressions.count # Count(Account.schedule.impressions) 我想在尽可能少的查询中做到这一点,也许也可以学到一些东西! 我喜欢效率和优化。

使用带有多个数据库表语句的`INNER JOIN`时意味着什么?

我正在使用Ruby on Rails 3.2.2和MySQL。 我有一个生成以下SLQ查询的方法( has_many :through ActiveRecord::Associations ): SELECT DISTINCT `articles`.* FROM `articles` INNER JOIN `articles_comments_associations` `comment_associations_articles` ON `comment_associations_articles`.`article_id` = `articles`.`id` INNER JOIN `articles_comments_associations` ON `articles`.`id` = `articles_comments_associations`.`article_id` WHERE `articles_comments_associations`.`comment_id` = 223 AND (articles_comments_associations.user_id IN (2)) 我想理解INNER JOIN ‘articles_comments_associations’ ‘comment_associations_articles’ ( 注意 : INNER JOIN有多个数据库表语句)以及SQL查询如何工作,因为我没有名为comment_associations_articles的数据库表 。 这是一个错误 (即使它按预期工作) ?

如何在MySQL / Rails中获取列的随机默认值

假设我有一个充满颜色值的mysql表,如“#EE3AA”。 我有一个具有颜色属性的模型,这是必须的(应该validation),但不一定需要由用户输入。 如果用户留空,我希望使用从另一个表中自动选择的随机值来填充它,但默认情况下并不总是相同的值。 我想,也许,我可以使用迁移来做到这一点,所以它是这样的: … :default => “values of the color column of the Colors table”.to_a.sample 就Ruby而言,我的想法可能不正确/合法。 在迁移文件中看到:default选项后,我认为每次创建模型时都会运行迁移文件。 但阅读米哈伊尔的评论,可能并非如此。 但无论如何,当用户没有提供任何表时,如何从另一个表中获取随机值?

MySQL + Rails:错误:150“外键约束不正确”

我试图迁移我的Rails MySQL数据库,我收到以下错误: ActiveRecord :: StatementInvalid:Mysql2 :: Error:无法创建表development 。 comments (错误:150“外键约束形成错误”):CREATE TABLE comments ( id int AUTO_INCREMENT PRIMARY KEY, comment varchar(255), user_id int, post_id int, created_at datetime NOT NULL, updated_at datetime NOT NULL,INDEX index_comments_on_user_id ( user_id ),INDEX index_comments_on_post_id ( post_id ),CONSTRAINT fk_rails_03de2dc08c FOREIGN KEY( user_id )REFERENCES users ( id ),CONSTRAINT fk_rails_2fd19c0db7 FOREIGN KEY( post_id )REFERENCES posts […]

Rails在分组后显示最新值

如果我有这个数据库表服务器 : date,server_id,server_name,server_status 我已经在我的控制器中设置: @servers = Server.all.group(:server_id) 在我看来,我有一个像这样的表: 如何在最近记录的日期显示服务器状态?

将一个大表分成多个表

rails app,我有一个表,数据已经有数亿条记录,我要将表拆分为多个表,这样可以加快读写速度。 我找到了这个gem章鱼 ,但他是一个主/从,我只是想拆分大桌子。 或者当桌子太大时我该怎么办?

无法使用mysql db在rails中启动服务器

在终端窗口中使用rails s或rails服务器时,我得到以下消息。 我在应用程序根目录中。 有任何想法吗? /Users/paul/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/lib/mysql2.rb:8:in `require’: dlopen(/Users/paul/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError) Referenced from: /Users/paul/.rvm/gems/ruby-2.0.0-p247/gems/mysql2- 0.3.13/lib/mysql2/mysql2.bundle Reason: image not found – /Users/paul/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle from /Users/paul/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/lib/mysql2.rb:8:in `’ from /Users/paul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `require’ from /Users/paul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `block (2 levels) in require’ from /Users/paul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `each’ from /Users/paul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `block in require’ from /Users/paul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `each’ from /Users/paul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `require’ from /Users/paul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler.rb:132:in `require’ from /Users/paul/Documents/Ruby/simple_cms/config/application.rb:7:in […]

mysql不尊重my.cnf中的wait_timeout设置

我在my.cnf中设置了wait_timeout并重新启动了服务器,但空闲连接的时间继续大于我设置的默认值100s。 任何想法为什么会这样? PS:我正在运行ubuntu 12.04和Mysql Server 5.5。 使用Rails 3和mysql2 gem。 mysql> SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST ORDER BY TIME DESC LIMIT 20; +——+——————+————————————-+————————–+———+——+——-+——+ | ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | +——+——————+————————————-+————————–+———+——+——-+——+ | 760 | user | ip-xxx-xxx-xxx-xxx.ec2.internal:45852 | x | Sleep | 4202 | | NULL | […]