logo
down
shadow

How do I map an association through a model's parents if it can only belong to one of them?


How do I map an association through a model's parents if it can only belong to one of them?

By : FaizRibic
Date : November 21 2020, 09:01 AM
around this issue If your shopping bag is a specific bag, then you can declare it as a subclass.
code :
class ShoppingBag < Bag
  // specific attributes
end
def bag
  super || shopping_bag.bag
end
validates :bag, absence: true, if: :shopping_bag
validates :shopping_bag, absence: true, if: :bag


Share : facebook icon twitter icon
How do I setup my association for Pages that are moveable/can belong to other Journals?

How do I setup my association for Pages that are moveable/can belong to other Journals?


By : Bart
Date : March 29 2020, 07:55 AM
Any of those help to satisfy requirement 1 you'd need a self reference from pages to pages. this problem is already answered here
You'd do something like this
code :
class Page < ActiveRecord::Base
  belongs_to :user
  belong_journal
  has_many :pageRelations, :foreign_key => "page_id", :class_name => "PageRelation"
  has_many :pages, :through => :pageRelations

  def addSubPage(page)
    # TODO: put in check that association does not exist
    self.pages << page
    page.pages << self
  end
end

class PageRelation < ActiveRecord::Base
  belongs_to :parentPage, :foreign_key => "page_id", :class_name => "Page"
  belongs_to :subPage, :foreign_key => "page_id", :class_name => "Page"  
end
page = Page.new
page.journal= Journal.new
sPage = Page.new
pPage = Page.new
pPage.addSubPage(sPage)
Activerecord/Datamapper - Have one child belong to many parents

Activerecord/Datamapper - Have one child belong to many parents


By : paul jay
Date : March 29 2020, 07:55 AM
it fixes the issue Yes, you would have to define many-to-many relationship between a Bookshelf and a Book. There are two ways to achieve this in Rails:
Option 1) Use has_and_belongs_to_many
code :
class Bookshelf < ActiveRecord::Base
  has_and_belongs_to_many :books
end

class Book < ActiveRecord::Base
  has_and_belongs_to_many :bookshelves
  has_many :reviews
end
class CreateBooksBookshelvesJoinTable < ActiveRecord::Migration
  def change
    create_table :books_bookshelves, id: false do |t|
      t.belongs_to :book, index: true
      t.belongs_to :bookshelf, index: true
    end
  end
end
class Bookshelf < ActiveRecord::Base
  has_many :books, through: :books_bookshelves
  has_many :books_bookshelves
end

class Book < ActiveRecord::Base
  has_many :bookshelves, through: :books_bookshelves
  has_many :books_bookshelves
  has_many :reviews
end

class BooksBookshelf < ActiveRecord::Base
  belongs_to :book
  belongs_to :bookshelf
end
class CreateBooksBookshelvesJoinTable < ActiveRecord::Migration
  def change
    create_table :books_bookshelves, id: false do |t|
      t.belongs_to :book, index: true
      t.belongs_to :bookshelf, index: true
      # add your custom columns here
    end
    add_index :books_bookshelves, [:book_id, :bookshelf_id], unique: true # to make sure you won't create duplicate records
  end
end
Rails - Can parent models validate attribute values of the models that belong to them?

Rails - Can parent models validate attribute values of the models that belong to them?


By : Aimul Khattak
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Take a look at 2.2 validates_associated explained on http://guides.rubyonrails.org/active_record_validations.html
A rough idea is something like this
Filtering models that belong to other models

Filtering models that belong to other models


By : Ben Kamble
Date : March 29 2020, 07:55 AM
around this issue Assuming params[:product][:product_category] is the ProductCategory ID.
code :
class ProductsController < ApplicationController
  def index
    if params[:product]
      @category = ProductCategory.find(params[:product][:product_category])
      @products = @category.products
    else
      @products = Product.all
    end
  end
end
MySQL: selecting joined rows, where children don't belong to a specific parents id

MySQL: selecting joined rows, where children don't belong to a specific parents id


By : user2567180
Date : March 29 2020, 07:55 AM
around this issue The question might sound a little confusing, I'll do my best to explain it. I have 4 tables:
code :
SELECT store_item_stock.id, store_item_stock.id_attribute, store_item_stock.stock 
FROM store_item_stock 
JOIN store_cat_attribute 
ON store_item_stock.id_attribute=store_cat_attribute.id_attribute
WHERE id_cat NOT IN (
SELECT store_item.id_cat 
FROM store_item JOIN store_cat_attribute 
ON store_item.id_cat=store_cat_attribute.id_cat 
WHERE store_item.id_cat=2 
GROUP BY store_item.id_cat);
Related Posts Related Posts :
  • Active Model Serializer - How do I include an attribute conditionally? Rails
  • Rails Fake-S3 on CircleCI Connection refused
  • Insert a variable for a label in Rails
  • Filter chain halted as :require_no_authentication rendered or redirected
  • Public Activity - Creating activities based on certain attribute changes
  • Turn off email sends from Devise in test (Rspec)
  • Attributes method explanation
  • Rails 4 Bootstrap DatePicker class today not working
  • Mongoid: Change(rename) foreign_key name without loosing data
  • Setting up controller and its actions for Admin login only, not for a Users?
  • How to use the HAML t() helper in RoR
  • How do I output a json dates array with RABL
  • Why does this FTP block read operation throw EOFError?
  • prawn rails download on open
  • Rails 4 deeply nested content tags and strings
  • Sending mail with Rails 4 in development environment
  • Mapping `has_many` in active admin
  • Rails - Use nested form to create new records
  • How to integrate elasticsearch with rails application specifically using "Elasticsearch" gem
  • Querying double has_many :through association
  • how to give whitespace in ruby slim
  • Mongoid, get distance from models in kilometers
  • can I get a data from file only one time in my gem similar to config/initializers in rails?
  • Calling a method from another controller in rails
  • Video content type is invalid in paperclip-ffmpeg
  • Website Speed With Heroku
  • adding Belongs to without "nil" in rails
  • Cloudfront returning 401 in response to requests
  • Should you use belongs_to if belonging to the object is optional?
  • Ordering a rails model with string outline notation
  • Ruby - Merge two arrays and remove values that have duplicate
  • Rails 4 - Using PUT instead of PATCH for updates
  • Rails last day of last week bug?
  • Mongodb querying for aggregation with count of multiple values
  • Bundler throws an error while creating rails new APP
  • Rails 4 - Devise: getting ActionController::UnknownFormat on signup
  • activeadmin: link_to in a helper does not appear in view
  • Virtual model attribute which links to a record from associated model
  • Travis CI cant resolve association in rails
  • How to add a custom route, controller, and action in Ruby on Rails?
  • Rails 4 session is not updated between requests
  • Input number multiplies as a string instead of number - Rails 4
  • Get enqueued_at inside scheduled Sidekiq job?
  • Update args of a scheduled job
  • Ruby on Rails, SQL Server order by and comparison
  • Devise Model unable to Work
  • Switching Heroku app to server in other region
  • Controller action for link shortening
  • Finding objects that are not associated in has_many_through
  • Ruby on Rails, SQL Server Query
  • Issues with Createing a form update button from a controller method in Rails 4
  • `<module:Rails>': superclass mismatch for class Server (TypeError)
  • Do I need a model to insert through IRB?
  • Items controller index method executes twice losing local or instance variable settings
  • Why submitting an invalid post breaks a micropost feed (Railstutorial chapter 11)?
  • Persist radio_button value on form_tag submit get method
  • The method to redirect from one page to other on ruby on rails
  • How to add an additional View to generated Scaffold and adding an action to it
  • Rails 4 - Custom validation method not being called
  • How to scope by associations
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co