ruby on rails Models Schema - ruby-on-rails

Hello every one I'm new to ruby on rails. I go through the following code in which I'm told that database schema is being loaded through this code:
Account.current = Question.find(2)
Question.last
I'm unable to understand about how schema is being loaded. what this code is actually doing. Please help

Start with Rails Guides (Active Record). Here is the documentation.
Also, look at AREL. Here is the link to it on GitHub.
This article explains how arel converts ruby queries into sql statements.

Related

Seed.rb in Ruby on Rails for creating database

I have two classes: Article(has_many comments) and Comment(belongs_to article). My database is empty, postgresql.
I need download information in db from seeds.yml using seed.rb. Code in seeds.rb must contain class HashWithIndifferentAccess.
I'm newbie in RoR and I cannot find some good information about it.

How can I edit existing activerecord objects in a sql database using ruby scripts?

How can activerecord objects be edited externally with scripts? I'm currently using the activerecord-import gem to insert objects into the database. Is it possible to edit existing records using this gem or using another tool or gem?
That's a bit unspecific. There are quite a few methods. Here's the best:
You can run rails on the console. Try irb in your project's directory, and you're on a REPL that allows you to run code live. Just try something like:
that_guy = User.find(4)
that_guy.name = "John"
that_guy.save!
(Replace User, 4 and name with a model you have, a record's id and some string attribute).
And of course you can also just run a file of ruby against your project: rails runner your_script.rb
And, if I misunderstood you: you can of course access the database through any other means/languages/libraries.

Rails with two different Databases

I have used two different databases for my Rails application: MongoDB and MsSQL using Mongoid and activerecord-sqlserver-adapter adapter respectively. Everything is well but there is a problem while generate Model.
The problem is "how can I generate the model that relates to MongoDB or MsSQL differently?"
For example: I want to generate People model relates to MongoID and Animal model with MsSQL. While I generate with command: rails g model Animal name:string it generates the model related to mongoid. How can I generate the model Animal with ActiveRecord that means related to MsSQL.
Please help me.
Thanks
Based on Using Active Record generators after Mongoid installation? I believe this should work:
rails g active_record:model Animal name:string
First let me just check that I've understood your question correctly:
You have 2 databases and a series of models/migrations, and you want a way to tell rails which database to use when running a migration and accessing the database using your model?
If I'm in the right area then you need to add a method to your migration which overrides the default connection() method in ActiveRecord::Migration.
def connection
ActiveRecord::Base.establish_connection(:conn_name).connection
end
Where :conn_name is the name you gave your connection settings in config/database.yml
within your models add the line
establish_connection :conn_name
to the top of your model file and the model will now know which DB to connect to.
So the quick and dirty way that I have handled this in the past (due to my dev team keeping mongoid in the gem file for legacy reasons) is to comment out the out mongoid when you have to do migrations run a bundle, generate and run you migration then uncomment and run bundle again. This is far from best practices but it should work.

Does Rails 4 have support for OR queries

So in Rails 4 the long desired feature to use not queries has been added.
Article.where.not(title: 'Rails 3')
Has similar support been added for or queries, or are they planning to make it. I couldn't find anything by browsing through the release notes.
Obviously I tried
Article.where(title: 'Rails 3').or(title: 'Rails 4')
But that does not work.
Article.where(title: ['Rails 3', 'Rails 4'])
is how you'd do that in Active Record.
It's not possible to replicate any arbitrary SQL query using "Rails-y" syntax. But you can always just pass in literal sql.
So you could also do:
Article.where("articles.title = 'Rails 3' OR articles.title = 'Rails 4'")
This now works in Rails 5:
Article.where(title: 'Rails 3').or(Article.where(title: 'Rails 4'))
Code example in Rails's source.
The most common alternative is already answer by #gregates
Recently there has been pull request in rails source
Add #any_of query method to active_record
Which adds or functionality to activerecord
the contributor has already created a gem incase its not accepted
its rails 3.2 and 4 compatible
https://github.com/oelmekki/activerecord_any_of
I havent tried it yet but soon wish to use it looks good to me.
I know that this is an old thread, but anyone else looking for a solution to this problem might find this code helpful:
Article.where(title: ["Rails 3", "Rails 4"])
Maybe that will help you get someone going in the right direction without having to risk sql injection.
It seems that the rails master branch is now supporting OR queries. https://github.com/rails/rails/pull/16052
I assume it will be in the framework with the next major release.
Rails 5 will support it but you can use this backport for Rails 4.2 : https://github.com/Eric-Guo/where-or

Ruby on rails -elasticsearch tire impoting existing db

I have a Ruby on rails 3.2 application. I want to enable text based search on a model that has a lot of data already populated in it. Suppose the name of the model class is Post. I am planning on using elasticsearch since I heard it is one of the best real-time search engines around and I am using tire gem so that my application can interact with elasticsearch.
As I am new to elasticsearch I am having trouble creating indices for the existing data for the model. I am using mongodb as the backend database. Can anyone tell me how to import the indices.
I have already tried
Tire.index "posts" do
import Post.all
end
The error that I got was :
BSON::InvalidObjectId: illegal ObjectId format: Career Guidance
from /Users/anirvan/.rvm/gems/ruby-1.9.3-p125/gems/bson-1.5.1/lib/bson/types/object_id.rb:126:in `from_string'
Can anyone help me out here ?
I use Mysql and this code in bash(from railscasts.com):
rake environment tire:import CLASS=Post FORCE=true
http://www.elasticsearch.org/guide/appendix/clients.html
tire: Ruby API & DSL, with full Rails ActiveModel compatibility and mongoid integration through mebla.
Try it, maybe help you.

Resources