associations

Rails 3 has_and_belongs_to_many creates checkboxes in view

痴心易碎 提交于 2019-12-06 09:49:21
问题 Based on following models class Company < ActiveRecord::Base has_and_belongs_to_many :origins end class Origin < ActiveRecord::Base has_and_belongs_to_many :companies end I want to have in my companies/_form a collection of checkboxes representing all origins. Don't know if the Company.new(params[:company]) in companies_controller#create can create the association between company and the selected origins? I'm running rails 3.0.0, what is the best way to achieve that? thanks for your insights

What would be the best way to design database and relationships for a website like Yelp or eBay (lots of categories and sub-categories)?

╄→гoц情女王★ 提交于 2019-12-06 08:59:53
问题 Do they have a table for all categories and another for all sub-categories (and another for the sub-sub-categories and so on), or what? How do all the levels go around communicating with each other? I'm a noob getting started on a project that might have that level of complexity and I am having a hard-time wrapping my head around that. I'm working with Rails but I'd also appreciate answers in database schemas, pointers to further reading etc. 回答1: I am assuming that you are dealing with

Rails: Finding all associated objects to a parent object

*爱你&永不变心* 提交于 2019-12-06 08:49:01
I have created a complex object in rails with a principle parent object "Resume" it has a number of child objects for each section("objective_section", "contact_section", etc), is there a way I can fetch all associated objects to the parent object Resume? If by fetch you mean load from the database all in one query, then sure: Resume.first(:include => [:objective_sections, :contact_sections]) # etc... If this is a common pattern and you want to DRY things up without much effort, you can throw this into a named_scope in your model: class Resume < ActiveRecord::Base has_many :objective_sections

belongs_to belongs_to association only no has_many or has_one

假如想象 提交于 2019-12-06 06:54:59
Can you have a belongs_to belongs_to relationship in Rails? Search results gave me two results. The fact that I can't find very much info on the subject, seems to indicate that it shouldn't be done or is bad practice. I asked yesterday about a has_many relationship, but thought because I couldn't find information on this, I would generate a question so it is easier for people to search for this in the future. I'm paraphrasing another users answer (I hope this is ok). A Shoe can have many Socks, but only one active Sock. Other Socks are inactive. All Socks are also unique with unique patterns.

Associate different models using sequelize?

不羁岁月 提交于 2019-12-06 06:28:43
Hi I am trying to associate my User model with login model and Question_details models.But if i am using the Question_details association then i am geeting eagerLodingError :user is not associated to login but if i am commenting it then it works fine so how can i associate it ? But if i am associating with User Model module.exports = (sequelize, DataTypes) => { var Users = sequelize.define('users', { name: { type: DataTypes.STRING(100) } phone: { type: DataTypes.BIGINT, unique: true } }, { freezeTableName: true }); Users.associate = function(models) { Users.hasOne(models.login, { foreignKey:

How to set up custom string foreign key in Rails 4?

独自空忆成欢 提交于 2019-12-06 06:16:07
问题 How to set up associations to set up properly has_one using string foreign_key? class Pharmaceutic < ActiveRecord::Base has_one :pharmaceutic_group, foreign_key: "code" end class PharmaceuticGroup < ActiveRecord::Base belongs_to :pharmaceutic, primary_key: 'code' end >> Pharmaceutic.last.pharmaceutic_group Pharmaceutic Load (0.3ms) SELECT `pharmaceutics`.* FROM `pharmaceutics` ORDER BY `pharmaceutics`.`id` DESC LIMIT 1 PharmaceuticGroup Load (0.3ms) SELECT `pharmaceutic_groups`.* FROM

rails, activerecord sum then order

戏子无情 提交于 2019-12-06 04:47:11
问题 i have a Job model that belongs_to User, and User has_many jobs. I want to create an AR query that calculates the total number of work days per user, then orders in Descending order. I have this so far, but is giving me an error: (column "Job.id" must appear in the GROUP BY clause or be used in an aggregate function) @work_days = Job.group(:user).order('SUM(total_days)') I can't seem to get the .order method to work - is there something I am missing? Thanks in advance! 回答1: You could write

Sequelize one to one relation

不羁岁月 提交于 2019-12-06 04:20:02
I have two models, Video and Frame. Frame.belongsTo(models.Video); Video.hasMany(models.Frame, { as: "Frames" }); I now need a way to specify first and last frame for a video, but can't get it to work. I tried this: Video.hasOne(Frame, { as: "FirstFrame" }); Video.hasOne(Frame, { as: "LastFrame" }); but that creates FirstFrameId and LastFrameId in the Frames table instead of the Videos table. I need to have video.get/setFirstFrame() and video.get/setLastFrame() functions available. How can I do this? Ellebkey You don't need to set belongTo and hasMany as you show at first. Use only one

Doctrine 2 Pagination with Association Mapping

二次信任 提交于 2019-12-06 03:29:44
I am wondering how can you paginate the results obtained from an entity association mapping in Doctrine 2? For example class Customer { /** * @OneToMany(targetEntity="Order") */ private $orders; } can be used as such: $customer->getOrders(); which will return a collection of Order objects. The problem is when there are a large number of order objects. We can use Doctrine\ORM\Tools\Pagination\Paginator when building custom queries, however I do not see any way to hook into query generation when utilising association mapping. class Paginator { /** * @param Query|QueryBuilder $query A Doctrine

Rails Associations Through Multiple Levels

百般思念 提交于 2019-12-06 03:28:54
I am relatively new to Rails and am working on a project that involves multiple, "nested" levels of data. I am having issues making the proper associations so I can get all the child elements of a model 3 levels higher. Here is an example of the models: class Country < ActiveRecord::Base has_many :states end class State < ActiveRecord::Base belongs_to :country has_many :cities end class City < ActiveRecord::Base belongs_to :state has_many :people end class Person < ActiveRecord::Base belongs_to :city end I have implemented a relationship in the Country model, has_many :cities, :through =>