problem about model-associations-Collection of common programming errors


  • dannymcc
    ruby-on-rails-3 model associations model-associations legacy-database
    I am working with a legacy database and am also reading through the Rails guides on associations.I have two models. A diary model and an animal model.diary.rbclass Diary < ActiveRecord::Baseestablish_connection :premvetattr_accessible :DiaryNo ,:DiaryName, :DiaryDate, :SlotHour, :TotalSlots, :BlockSlots, :BlockBooked, :FreeSlot, :BookedSlotself.table_name = ‘diary’self.primary_key = ‘DiaryNo’has_many :animals, :foreign_key => ‘DiaryQueue’ endanimal.rbclass Animal < ActiveRecord::Baseestablish_connection :premvetself.table_name = ‘animal’self.primary_key = ‘PVID’attr_accessible :AddedBy, :Age, :AnimalBFAmount, :AnimalBalance, :AnimalName, :Archive, :BillType, :Breed, :ChronicStatus, :Class, :Classification, :ClientKey, :Colour, :Date1, :DateOfBirth, :DateofBirth, :Dead, :DiaryQueue, :DiscField, :DrugsAtCost, :DrugsNoVAT, :ESDAmount, :ESDType, :FNote, :FirstRegisteredDate, :Height, :IDNumber, :Insured, :InsuredWith, :IsClient, :IsClientDate, :Is

  • gorlaz
    ruby ruby-on-rails-3 associations model-associations
    I’m very green when it comes to RoR and am having trouble trying to figure out whether my issue is caused by a problem in my model associations or whether I’m just not using the right syntax to access the data.A user can have many budgets. Each budget is comprised of multiple detail lines. I thought there wasn’t a need for the user id in budget_details since it’s captured in budget and so can be inferred through the relationship between the three (maybe?!)In the budget_details index I want to be able to include the users name; I’ve got it to work in the ‘show’ view but not the index.I did use a scaffold to set these up, so I know there’s a lot of crud there, I was just trying to do an example before moving to a new project to do it for real.The actual error is;NoMethodError in

  • Ben G
    ruby-on-rails-4 model-associations
    I have a Location model and an Answer model and I’m trying to join the two, so that when an Answer is created, it is associated with the Location. I have added in the appropriate has_one and belongs_to associations, added a migration to add Locations id to the Answer table.Trouble is when an Answer gets created, the ‘location_id’ is nil. Any help would be greatly appreciated. Here’s my files:Location Modelclass Location < ActiveRecord::Baserequire ‘Geoip-rails’has_one :answerdef self.get_location geoip = GeoIP.new(“lib/GeoLiteCity.dat”)l = Location.newl.city = geoip.country(‘78.151.144.93’).city_namel.saveend endAnswer Modelclass Answer < ActiveRecord::Basebelongs_to :

  • Smooth
    ruby-on-rails-3 forms model-associations
    I have access to @result in my view result.html.erb. @result is a Movie object. I’d like to create comments for this @result object through a form. I’m placing the comment box (form) inside result.html.erb.I’m new to the syntax for forms. I’m also confused about where to direct the form itself to after submitting. Do I need to create a new controller all together for the comments with a create actio

  • ibechev
    ruby-on-rails-3.1 model-associations partialview
    I have these two models in my Ruby on Rails application – Artist and Song. They are associated as follows:class Artist < ActiveRecord::Basehas_many :songsattr_accessible :artist_nameandclass Song < ActiveRecord::Basebelongs_to :artistattr_accessible :title, :track_URL, :artist_idI have this code in views/artist/show.html.erb:
    Jim
    ruby sinatra model-associations ruby-datamapper
    I am brand new to Ruby and even newer to DataMapper, or any ORM for that matter. I come from a Perl background and am not really an OOP type of developer. So I’m wandering in unknown territory here. Apologies for the long-winded question…In this project, I have the concept of deviceclasses and devices which will be mapped under the deviceclasses. Deviceclasses need to be able to have child deviceclasses. The common root deviceclass names (in other words the root from which all deviceclasses come) are called “FOO” or “BAR” (in this example code) and each of those can have an arbitrary set of children devicesclasses. Finally, deviceclasses eventually contain devices.So: Deviceclasses have many deviceclasses Deviceclasses have many devices A deviceclass has one deviceclass_name many devices belong to a deviceclassSo, IE:FOOJOHNSHOUSEUPSTAIRSdevice1device2DOWNSTAIRSdevice1device2MANYHOUSESJOEGARAGEdevice1device2SUZYBEDROOMdevice1device2device3TIMLIVINGROOMdevice1ARBITRARYdevice1SOMEPLACEdevice1device2 BARENGLANDLONDONMYHOUSEBEDROOMdevice1device2device3And here’s where I’m ge
    user2325970
    ruby-on-rails query activerecord model-associations
    I’m trying to create two associations that are identical in nature but have two different names because they mean different things. I have a feeling that this is a syntax issue I can’t quite get right. I’d rather not rewrite the base level models, but I’m open to suggestions. As far as the modeling goes: Raids have players. Raids have leaders (which are players). I want this players to raids association to exist in the players_raids table and leaders_raids table. The two sets of method calls need to be complementary: players.raids and raids.players, and players.raids_led and raids.leaders. player.rb:class Player < ActiveRecord::Basehas_many :players_raids, class_name: ‘PlayersRaids’has_many :raids, through: :players_raidshas_many :leaders_raids, class_name: ‘LeadersRaids’has_many :raids_led, through: :leaders_raids, source: :leader, foreign_key: ‘leader_id’ endraid.rb:class Raid < ActiveRecord::Basehas_many :players_raids, class_name: ‘PlayersRaids’has_many :players, through: :players_rai
    tester123
    mysql ruby-on-rails ruby-on-rails-3 model-associations
    I can’t seem to understand how this association should look and work. I know I have not been very rails like with the naming convention, but neither is the source DB where I get the data, I do not have the freedom to change the primary keys as they relate also to the real source database, which I use ETL to load into my rails database.These are my models:class Eclass < ActiveRecord::Basehas_many :elinks, :foreign_key => :concept_idhas_many :eproperties, :through => :elinks endclass Elink < ActiveRecord::Basebelongs_to :eclass, :foreign_key => :concept_idbelongs_to :eproperty, :foreign_key => :pconcept_id endclass Eproperty < ActiveRecord::Basehas_many :elinks, :foreign_key => :pconcept_idhas_many :eclasses, :through => :elinks endAnd these are the MySQL table schemas:mysql> DESC eclasses; +————–+————–+——+—–+———+—————-+ | Field | Type | Null | Key | Default | Extra | +————–+————–+——+—–+———+—————-+ | id | int(11) | NO | PRI | NULL | auto_increment | | class_id | varchar(255) | YES | | NULL | | | concept_id | varchar(255) | NO | PRI | 1 | | | concept_name | varchar(255) | YES | | NULL | | | language_id | varchar(255) | YES | | NULL | | | created_at | datetime | YES | | NULL | | | updated_at | datetime | YES | | NULL | | +————–+————–+——+—–+———+—————-+ 7 rows in set (0.00 sec)mysql> DESC elinks; +————-+————–+——+—–+———+—————-+ | Field | Type
    pixelearth
    ruby-on-rails activerecord model model-associations
    This association:class Business < ActiveRecord::Basehas_one :map_address, :as=>:addressable, :class_name => ‘Address’, :conditions => {‘addresses.map_address’=>true}, :dependent => :destroy endCauses this error:ruby-1.8.7-p334 :005 > n = Business.new ruby-1.8.7-p334 :006 > n.build_map_address ActiveRecord::UnknownAttributeError: unknown attribute: addresses.map_addressThe code used to read :conditions => {:map_address=>tru
    Keith Johnson
    activerecord model foreign-keys has-many-through model-associations
    I’m trying to run a functional test on my PollsController and keep getting this error:block in assert_valid_keys’: Unknown key: through (ArgumentError)I’m wondering if its because I have a dependent destroy on the Question model and a has_many relationship ship through it to
    endo.anaconda
    Ralph Smith
    ruby-on-rails model-associations
    I thought I could beat this to death by systematically moving through options. It Won.index.html.erb and form.html.erb work as things are now.I have a many to many relationship between Bids and Contacts, with bids_contacts in between with bid_id and customer_contact_id in that table.Here’s the contacts then bids m
    user1149642
    ruby-on-rails-3 nested-attributes model-associations sti
    I am trying to implement a ‘mixed model’ scenario where I have a base table ‘People’ and a class of ‘Person’. From this through STI I have an ‘Employee’ class. An employee can have attributes which are outside of the generic ‘Person’ class as described in the link below. The STI portion of the setup is working. I’m having problems with the details associations/linking saving.[question:] http://stackoverflow.com/questions/1634668/multiple-table-inheritance-vs-single-table-inheritance-in-ruby-on-rails?rq=1I am having trouble with the detail forms for StaffDetail. I am getting the following error:ActiveRecord::UnknownAttributeError in PeopleController#createunknown attribute: employee_detailsParameters:{“utf8″=>”✓”,”authenticity_token”=>”wrUt50Qt9G6hVd8SsMAY1fVno9npm4w6QMC6+uREj2E=”,”employee”=>{“honorific_prefix”=>””,”first_name”=>””,”known_as”=>””,”middle_names”=>””,”family_name”=>””,”honorific_suffix”=>””,”dob”=>””,”gender”=>”0″,”employee_details”=>{“department”=>”Accident & Emergency”,”employee_number”=>””}},”commit”=>”Create Employee”,”person_type”=>”Employee”}As we have a ‘has_one’ ‘belongs_to’ association I have the following questions:Where does the attr_accessible ’employee_details_attributes’ declaration go? In the Parent model (‘Person’)? In the Employee STI table/model (which inherits from Person)? In the ‘EmployeeDetail’ model? Should the declaration for has_one not be has_one :detail, :class_name => “#{self.name}Detail”[no s]? Do I need to use ._build in the controller or does the accepts_nested_attributes_for take care of that? If so which one?I can get the employee detail fields to display, they just don’t save/update.I already have in the ‘Person’ class nested_attributes for peoples addresses, but thats a has_many’ / ‘polymorphic’ relationship.Just trying to get the detail down now and display it in the same form.In addition to the above questions, I cannot understand why in the outline at the link, one of the detail classes specifies :class_name and other doesn’t`class SMSDetail < ActiveRecord::Basebelongs_to :SMS, :class_name => “SMS” # sms specific validations… endclass TwiterDetail < ActiveRecord::Basebelongs_to :twitter# twitter specific validations… end `Happy to post my code in a Gist if necessary.Has anyone successfully implemented this as it stands?When I build my associations in the console I get the following errors:rails c Loading development environment (Rails 3.2.5) 1.9.3-p125 :001 > Employee.allEmployee Load (0.3ms) SELECT “people”.* FROM “people” WHERE “people”.”type” IN (‘Employee’)EmployeeDetail Load (0.2ms) SELECT “employee_details”.* FROM “employee_details” WHERE “employee_details”.”employee_id” IN (5, 7)=> [#

    “AdminUser” endclass AdminUser < ActiveRecord::Baseattr_accessible :first_name, :last_name, :usernamehas_and_belongs_to_many :pagesscope :named, lamda {|first,last| where(:first_name => first, :last_name => last)} endclass Section < ActiveRecord::Basebelongs_to :page endwith these migrations for Pages, Users and Sections:class CreatePages < ActiveRecord::Migrationdef changecreate_table :pages do |t|t.references :subjectt.string “name”t.string “permalink”t.integer “position”t.boolean “visible”, :default => falset.timestampsendputs “*** About to add an index ***”add_index(“pages”, “subject_id”)add_index(“pages”, “permalink”)end endclass CreateUsers < ActiveRecord::Migrationdef changecreate_table :users do |t|t.string “first_name”, :limit => 25t.string “last_name”, :limit => 50t.string “email”, :default => “” , :null => falset.string “password”, :limit => 40t.timestampsendend endclass CreateSections < ActiveRecord::Migrationdef changecreate_table :sections do |t|t.string “name”t.integer “position”t.boolean “visible”, :default => falset.string “content_type”t.text “content”t.timestampsendputs “*** About to add an index ***”add_index(“sections”, “name”)end endIn the process of trying to create a new user for the AdminUser using the rails console, I get this Object doesn’t support #inspect error:1.

Originally posted 2013-11-09 23:12:40.