Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sahilbansal17/rails_interview_questions

Ruby on Rails Interview Questions
https://github.com/sahilbansal17/rails_interview_questions

Last synced: about 2 months ago
JSON representation

Ruby on Rails Interview Questions

Awesome Lists containing this project

README

        

# Rails_Interview_Questions
Ruby on Rails Interview Questions

## Difference between Gemfile and Gemfile.lock

Gemfile and Gemfile.lock

Gemfile:

• The Gemfile is a file used by Bundler in Ruby projects to manage gem dependencies.
• It lists the gems required for the application, specifying versions or version constraints.
• It allows specifying groups of gems (e.g., :development, :test) to be used in different environments.

Gemfile.lock:

• The Gemfile.lock is generated by Bundler to record the exact versions of all gems and their dependencies.
• It ensures that the same versions of gems are used when the application is deployed or shared.
• This file is crucial for consistent environments across different setups (development, testing, production).

## Difference Between has_many :through and has_and_belongs_to_many

has_many :through:

• Used to set up a many-to-many connection with another model through a third model (a join model).
• Allows for more flexibility as the join model can have additional attributes and validations.
• Example:

```ruby
class Author < ApplicationRecord
has_many :authorships
has_many :books, through: :authorships
end

class Authorship < ApplicationRecord
belongs_to :author
belongs_to :book
end

class Book < ApplicationRecord
has_many :authorships
has_many :authors, through: :authorships
end
```

has_and_belongs_to_many (HABTM):
• Used to set up a many-to-many connection directly without a join model.
• Simpler to set up but lacks the flexibility of adding additional attributes or validations to the join table.
• Example:

```ruby
class Author < ApplicationRecord
has_and_belongs_to_many :books
end

class Book < ApplicationRecord
has_and_belongs_to_many :authors
end
```